Friday, 13 May 2016

Hibernate tutorial with xml configurations

Background

Hibernate is Java ORM (Object relation mapping) tool. It like other ORMs help map Java domain objects with tables in relational databases. It removes the overhead of using the underlying JDBC calls. It supports CRUD operations across all major relational databases. It supports transaction management and many other features. In this post we will see a demo example of hibernate application.


Architecture


Setup

I am simply going to create a simple standalone Java application using hibernate that used oracle database to connect to.  Also I am using Eclipse IDE with Apache Ivy dependency management tool. So create a project called HibernateDemo. It's structure will be as follows - 



Next lets create required files one by one. Lets start with the table creation. Following SQL should create the desired table for us in oracle - 

 CREATE TABLE Employee (

  id number(10) NOT NULL PRIMARY KEY,

  name varchar2(20) DEFAULT NULL,

  gender varchar2(20) DEFAULT NULL,

  accesstime_time DATE DEFAULT (sysdate)

);

 Next you will need some dependency jars like hibernate, slf4j etc. Add following dependency in ivy file - 

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
   Licensed to the Apache Software Foundation (ASF) under one
   or more contributor license agreements.  See the NOTICE file
   distributed with this work for additional information
   regarding copyright ownership.  The ASF licenses this file
   to you under the Apache License, Version 2.0 (the
   "License"); you may not use this file except in compliance
   with the License.  You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing,
   software distributed under the License is distributed on an
   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   KIND, either express or implied.  See the License for the
   specific language governing permissions and limitations
   under the License.    
-->
<ivy-module version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="http://ant.apache.org/ivy/schemas/ivy.xsd">
    <info
        organisation="ofsg.com"
        module=""
        status="integration">
    </info>
    
    <dependencies>
        <dependency org="org.hibernate" name="hibernate-core" rev="5.1.0.Final"/>
        <dependency org="org.slf4j" name="slf4j-simple" rev="1.7.21"/>
    </dependencies>
    
    
</ivy-module>

You will also need to manually add oracle OJDBC jar in the classpath.

Java Code and configurations


Next lets create our persistent Java class - Employee.java


package com.osfg.models;


import java.util.Date;

/**
 * 
 * @author athakur
 * 
 */
public class Employee {

    private int id;
    private String name;
    private String gender;
    private Date accessTime;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Date getAccessTime() {
        return accessTime;
    }

    public void setAccessTime(Date accessTime) {
        this.accessTime = accessTime;
    }

}



Next lets create configuration files that map this class to the database table - employee.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.osfg.models.Employee" table="EMPLOYEE">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="assigned" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <property name="gender" type="java.lang.String">
            <column name="GENDER" />
        </property>
        <property name="accessTime" type="timestamp">
            <column name="ACCESS_TIME" />
        </property>
    </class>
</hibernate-mapping>



NOTE : Notice the generator class. It is set to assigned so that you manually assign the primary key. There are many generator classes such as assigned (It is used if id is specified by the user), increment, hilo, sequence, native etc.

Now lets see the hibernate configuration file that has database connection details - hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>  
<!DOCTYPE hibernate-configuration PUBLIC  
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  
<hibernate-configuration>  
  
<session-factory>  
<property name="hbm2ddl.auto">update</property>  
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>  
<property name="connection.url">jdbc:oracle:thin:@localhost:1699/test</property>  
<property name="connection.username">system</property>  
<property name="connection.password">test</property>  
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>  
<mapping resource="/com/osfg/resources/employee.hbm.xml"/>  
</session-factory>  
  
</hibernate-configuration>  


And now finally write the demo code to demonstrate the functionality - HibernateDemo.java

package com.osfg.main;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.osfg.models.Employee;

/**
 * 
 * @author athakur
 * 
 */
public class HibernateDemo {

    public static void main(String args[]) {

        Configuration cfg = new Configuration();
        cfg.configure("/com/osfg/resources/hibernate.cfg.xml");
        SessionFactory sFactory = cfg.buildSessionFactory();
        Session session = sFactory.openSession();
        Transaction transaction = session.beginTransaction();

        Employee newEmployee = new Employee();
        newEmployee.setId(1);
        newEmployee.setName("Aniket");
        newEmployee.setGender("male");
        newEmployee.setAccessTime(new Date());

        session.persist(newEmployee);

        transaction.commit();
        session.close();

        System.out.println("Employee record successfully saved");

    }

}

And you should be all set. Just run as any other Java application.






Related Links


Tuesday, 10 May 2016

Hide variable from console access - Javascript Closures and self executing functions

Background

I came across javascript closure when I was searching solution for an interesting question - can variable be private in javascript. Let me give an example to demonstrate this further. Consider following Javascript - 


<script>
var counter = 0;

function increment() {
    counter++;
    console.log('After increment counter : ' + counter);
}

function decrement() {
    counter--;
    console.log(' After decrement counter : ' + counter);
}
</script> 

Now from console in your browser try to access counter variable. You should be able to access it. You can also try to print counter value after executing increment and decrement methods from console.

Couple of notes before we proceed to see the output on console.
  1. In above code counter is a global variable.
  2. In a web page global variables belong to window object. So you can also access it using window.counter.
  3. Global variables can be accessed/changed by all scripts in a page.
  4.  A local variable can only be used inside the function where it is defined. It is hidden from other functions and other scripting code.
  5. Global and local variables with the same name are different variables. Modifying one, does not modify the other.  
  6. Variables created without the keyword var, are always global, even if they are created inside a function.

Now the question is we don't want to expose counter variable. What can we do? Closures to the rescue.

Javascript closure

Now see the javascript code below - 

<script>

var increment, decrement, printCounter;

(function () {
    var counter = 0;
    increment = function() {
        counter++;
        console.log('After increment counter : ' + counter);
    };
    decrement = function() {
        counter--;
        console.log(' After decrement counter : ' + counter);
    };
    printCounter = function() {
        console.log('counter : ' + counter);
    };
})();

</script>

Now repeat above operation. You cannot access counter variable now. Since it is define inside the closure it's scope is limited to that. functions on the other hand are global, so those you can use.


Noticed the following syntax?

(function(){
    //Bunch of code...
})();


It's called self executing function. It is executed only once when page loads.



So A closure is a function having access to the parent scope, even after the parent function has closed. 
If you just have one method you can also do something like -

 var increment = (function () {
    var counter = 0;
    return function () {return counter += 1;}
})();

increment();
increment();
increment();
// the counter is now 3 



Related Links

Sunday, 8 May 2016

Understanding Tries data structure

Standard Tries

The standard Trie for a set of strings S is an ordered tree such that
  • Each node but the node is labelled with a character
  • The children of a node are alphabetically ordered
  • The path from external nodes to the root yield the String of S.

For example see following picture -



How would you represent this in code? Each node will have an array of size 26 (assuming all english characters) where each index of array will store pointer to next node which is essentially next character of the string.

Running time for operations

  • A standard trie uses O(W) space.
  • Operations find, insert, delete take time O(dm) each where
    • w = total size of strings in S,
    • m = size of string involved in operation
    • d = alphabet size

Compressed Tries

  •  Trie with nodes of degree at least 2
  • Obtained from standart trie by compressing chains of redundant nodes.


 Applications

Trie has many useful applications like
  • Find first prefix in a text (pattern matching)
  • or auto complete a text (which is why it can be used in a search engine). The index of a search engine is stored into a compressed trie. Each leaf of trie is associated with a word and has a list of pages (URLs) containing that word called occurrence list. Trie is kept in memory while the occurrence lists are kept in external memory and are ranked by relevance.
  • Suffix tree (rooted directed tree whose edges are labelled with non empty substrings of S)


  • The suffix tree for a text X of size N from an alphabet of size d stores all the N suffixes of X is O(N) space. NOTE : There might be a case where a suffix is a prefix of another suffix in which case the suffix will end on an internal mode. To counter this case you can end the alphabet with a special character eg. $. Time complexity to build suffix tree - O(N^2)



Related Links



Sunday, 1 May 2016

Database Normalization

Background

Database Normalization is a technique to store data in a database in most efficient and organized manner possible. The main aim is to eliminate data redundancies and undesirable characteristics like 
  • Insertion Anomaly
  • Update Anomaly
  • Deletion Anomaly
Database normalization is step by step process which we will look into shortly.


Problems faced without database normalization

Lets say you have following table - 

Employee_ID Name Skill level Team
1 John A Team C
2 Sam A+ Team B
3 Margaret B Team A
4 John A Team A



 Lets analyze above table data and see whats wrong with it .

  • Lets saw a new Employee is hired. When you make entry for him in this table his skill level will be null (as his skill sets are not yet tested ) and also his team (which may be determined by his skill set and level). This will result into Insertion Anomaly.
  • Now lets say John has become proficient in work and his skill level need to be updated. We will need to update all rows corresponding to John (He may be part of different teams) to keep the data consistent. This is called Update Anomaly.
  • Lets say Sam drops out of a team temporarily then we will have to delete the entire row (which inturn will delete employee details with it). This is called Deletion Anomaly.



Now lets see how we can resolve these anomalies.

Normalization Steps are as follows -

  1. First Normal Form (1NF)
  2. Second Normal Form (2NF)
  3. Third Normal Form (3NF)
  4. BCNF 
Now lets see these in detail -

First Normal Form (1NF)

A relation is in first normal form if and only if the domain of each attribute contains only atomic (indivisible) values, and the value of each attribute contains only a single value from that domain. (Wiki)

So basically a column can have set of atomic (cannot be broken down) values and each row can only have a single value from this set. So something like below is not allowed in 1 NF - 




Name Skill level Team
John A Team C, Team C
Sam A+ Team B
Margaret B Team A

Instead following table is in 1 NF  -

Name Skill level Team
John A Team C
Sam A+ Team B
Margaret B Team A
John A Team A

Second Normal Form (1NF)

 A table that is in first normal form (1NF) must meet additional criteria if it is to qualify for second normal form. Specifically: a table is in 2NF if it is in 1NF and no non-prime attribute is dependent on any proper subset of any candidate key of the table. 

A non-prime attribute of a table is an attribute that is not a part of any candidate key of the table. (Wiki)


In above table which is in 1 NF you can see that the candidate key (primary key consisting of multiple columns) is {Name,Team} However Name -> Skill level. So it violated 2 NF which states a non prime key (Skill level) should not depend on any proper subset of candidate key (Name).

So we split the table with the subset and dependent column to new table. So that the tables now become -

Name Skill level
John A
Sam A+
Margaret B
and
Name Team
John Team C
Sam Team B
Margaret Team A
John Team A

Third Normal Form (3NF)

 3NF ensures the entity is in second normal form, and  all the attributes in a table are determined only by the candidate keys of that table and not by any non-prime attributes. (Wiki)


Consider following table


Name Skill level Address Zip Code
John A ADDR 1 123
Sam A+ ADDR 2 345
Margaret B ADDR 3 156

This is in 2 NF but not in 3 NF as Zip Code (non prime key) -> Address (non prime key) (Candidate key - Name )So we need to move this dependency (also called transitive dependency)  to new table with Zip code as primary key.


Resultant tables will be -



Name Skill level Zip Code
John A 123
Sam A+ 345
Margaret B 156

and

Zip Code Asdress
123 ADDR 1
345 ADDR 2
156 ADDR 3



Boyce-Codd Normal Form (BCNF)

 For table to be BCNF compliant it should satisfy -

  • For any non-trivial functional dependency, X → A, X must be a super-key. OR
  • If X → Y is a trivial functional dependency then (Y ⊆ X)


NOTE : A superkey is a combination of columns that uniquely identifies any row within a relational database management system (RDBMS) table. A candidate key is a closely related concept where the superkey is reduced to the minimum number of columns required to uniquely identify each row.


To Sum it up -


Friday, 29 April 2016

Box Ball Logic Problem

Question

There are three bags.

  1. The first bag has two blue rocks. 
  2. The second bag has two red rocks. 
  3. The third bag has a blue and a red rock.
All bags are labeled but all labels are wrong.You are allowed to open one bag, pick one rock at random, see its color and put it back into the bag, without seeing the color of the other rock.

How many such operations are necessary to correctly label the bags ?




Solution

Just one!

You pick a ball from the bag labelled "blue and red" ... if its blue then that's the 2 blue rocks bag and likewise for the red. ( since all bags are labelled wrong the one reading blue and red would not actually contain a blue and red ball. So it label will be whatever color of ball comes from it) Next if we get a blue ball from the blue and red labelled bag we know that the bag labelled red has got to be blue and red since we have already used up the blue label and since all bags are labelled wrongly it cannot be red.
t> UA-39527780-1 back to top