Learn Generator Classes in Hibernate
Hibernate Generator class is important sub-element of id which is used in every hibernate application. It is responsible for generating unique indentifier for the objects of persistent class.
Hibernate Generator Classes
Hibernate generators represent a primary key in the database and are one of the most core concepts in hibernate framework. Hibernate provides different types of predefined generator classes. A generator class is used to provide strategy to generate an Id for an object, which is about to be inserted in the database table as a primary key.
- assigned
- increment
- sequence
- hilo
- identity
- native
- foreign
- uuid
How to use Hibernate Generator Classes ?
Hibernate uses <generator> tag to represent the generator. it is a sub-element of <id> tag. We have to define these elements in the Book.hmb.xml mapping file we have already created in previous example.
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="entity"> <class name="BookEntity" table="book"> <id name="id" type="java.lang.Integer"/> <property name="bookName" column="book_name"/> <property name="authorId" column="author_id"/> </class> </hibernate-mapping>
Hibernate assigned generator
It is the default generator in the hibernate. If there is no generator defined hibernate considers this generator. The assigned generator. The assigned class will return the same id set by the programmer and will store in the database table.
Book.hmb.xml
<id name="id" type="java.lang.Integer"/>
or
<id name="id" type="java.lang.Integer"> <generator class="assigned"/> </id>
both the above syntaxes are same for the assigned generator. We can use any of them.
Hibernate assigned generator using annotation
If you are using annotation configuration @Id will be used for marking id as identifier as well ass assigned generator.
@Id private Integer id;
Hibernate increment generator
The Increament generator class of hibernate is responsible for finding the max value of existing ids in database table and then increament it by one (max(id)+1)
and then returns the id value. Make sure you have previously created project in IDE
Replace the Book.hmb.xml with the following code.
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="entity"> <class name="BookEntity" table="book"> <id name="id" type="java.lang.Integer"> <generator class="increment" /> </id> <property name="bookName" column="book_name"/> <property name="authorId" column="author_id"/> </class> </hibernate-mapping>
and replace the main method with the following code.
public static void main(String[] arg) { try { StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build(); Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build(); SessionFactory factory = meta.getSessionFactoryBuilder().build(); Session session = factory.openSession(); session.beginTransaction(); BookEntity book = new BookEntity(); book.setBookName("C Book"); book.setAuthorId(1); //book.setId(3); session.save(book); session.getTransaction().commit(); System.out.println("Data Saved"); } catch (Exception e) { e.printStackTrace(); } }
Hibernate Increament generator using annotation
To use increament generator class in hibernate annotation configuration use the following code with identifier
@Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id;
Hibernate sequence generator
Sequence generator reads next value of a database sequence and then returns that value as id to the hibernate. We can create a sequence in the database by using the below syntax.
<id name="id" type="java.lang.Integer"> <generator class="sequence"></generator> </id>
Hibernate sequence generator using annotation
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE) private Integer id
Hibernate hilo generator
Hibernate hilo generator uses high and low algorithm to generate the id of type short, int and long.
<id name="id" type="java.lang.Integer"> <generator class="hilo"></generator> </id>
Hibernate identity generator
Hibernate native uses is a database dependent generator. It will work on some databases only like it works on Sybase, My SQL, MS SQL Server, DB2 and HypersonicSQL and not in Oracle database.
<id name="id" type="java.lang.Integer"> <generator class="identity"></generator> </id>
As identity generator class is called, it reads an auto-incremented column algorithm of the database and take the auto-incremented value and returns it to hibernate. Auto-incremented algorithm doesn’t exist in all the databases, hence identity generator is a database dependent generator
Hibernate identity generator using annotation
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id;
Hibernate native generator
Hibernate native uses identity, sequence or hilo depending on the database type.
<id name="id" type="java.lang.Integer"> <generator class="native"></generator> </id>
Hibernate foreign generator
Hibernate foreign generator uses the id of another associated object, used with <one-to-one> association.
Hibernate uuid generator
Till now any generator we have discussed work with number as primary key but if your primary key is String then we can use UUID generator class (Universal Unique Identifier). It uses 128-bit UUID algorithm to generate the id.
<id name="id" type="java.lang.String"> <generator class="uuid"/> </id>
Aamir Shayan
Software Engineer