show
hide

Java Hibernate Tutorial Part 6

In this video tutorial I will be showing how to generate the primary key automatically using the hibernate framework . The basic way of doing this is by using the generator property.We have to specify this in a hibernate mapping file in the identifier column, that is




    
    
    
    



so instead of using generator class “assigned” as we did in all our previous programs we will keep our generator class as ”increment” which will generate the id value by incrementing the id value of the previous record


so every time we insert a record we will get an incremented value for example if I am inserting a new record and the last id which was inserted is 10 so the new id which will be generated will be 11


Now the generator class can various class values and they are


In the video I will show how to use the sequence and increment generator values I will also show the use of the unsaved-value property in the video. The unsaved-value is used when we use the save or update method saveOrUpdate() becomes save() if id value in the object matches with the unsaved value attribute saveOrUpdate becomes save() else it becomes update().
Generator Description
increment It generates identifiers of type long, short or int that are unique only when no other process is inserting data into the same table. It should not the used in the clustered environment.
identity It supports identity columns in DB2, MySQL, MS SQL Server, Sybase and HypersonicSQL. The returned identifier is of type long, short or int.
sequence The sequence generator uses a sequence in DB2, PostgreSQL, Oracle, SAP DB, McKoi or a generator in Inter base. The returned identifier is of type long, short or int
hilo The hilo generator uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a table and column (by default hibernate_unique_key and next_hi respectively) as a source of hi values. The hi/lo algorithm generates identifiers that are unique only for a particular database. Do not use this generator with connections enlisted with JTA or with a user-supplied connection.
seqhilo The seqhilo generator uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a named database sequence.
uuid The uuid generator uses a 128-bit UUID algorithm to generate identifiers of type string, unique within a network (the IP address is used). The UUID is encoded as a string of hexadecimal digits of length 32.
guid It uses a database-generated GUID string on MS SQL Server and MySQL.
native It picks identity, sequence or hilo depending upon the capabilities of the underlying database.
assigned lets the application to assign an identifier to the object before save() is called. This is the default strategy if no element is specified.
select retrieves a primary key assigned by a database trigger by selecting the row by some unique key and retrieving the primary key value.
foreign Used where we use the identifier of another associated object generally in one to one association.

In the video I will show how to use the sequence and increment generator values I will also show the use of the unsaved-value property in the video. The unsaved-value is used when we use the save or update method saveOrUpdate() becomes save() if id value in the object matches with the unsaved value attribute saveOrUpdate becomes save() else it becomes update().


Program Code


1.Student.hbm.xml




    
    
    seq_student
    
    
    


 

Alternative Student.hbm.xml while using increment





    
    
    
    
    


        
2.Student.java
import java.io.Serializable;
public class Student implements Serializable{
 private int id;
 private String name;
 public Student(){}
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;
}
}
3.InsertStudent.java
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class InsertStudent {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Configuration cfg = new Configuration();
        cfg.configure(“Hibernate.cfg.xml”);
        SessionFactory sf = cfg.buildSessionFactory();
        Session s = sf.openSession();
        Transaction tx = s.beginTransaction();
        Student s1 = new Student();
        s1.setId(5);
        s1.setName(“batt”);
        s.saveOrUpdate(s1);
        s.flush();
        tx.commit();
        s.close();
    }
    
4.Hibernate Configuration same as previous tutorial
Press Esc to close