Using Hibernate <generator> to generate id incrementally
As we have seen in the last section that the increment class generates identifiers of type long, short or int that are unique only when no other process is inserting data into the same table. In this lesson I will show you how to write running program to demonstrate it. You should not use this method to generate the primary key in case of clustured environment.In this we will create a new table in database, add mappings in the contact.hbm.xml file, develop the POJO class (Book.java), write the program to test it out.
Create Table in the mysql database:User the following sql statement to create a new table in the database. CREATE TABLE `book` (
`id` int(11) NOT NULL default '0',
`bookname` varchar(50) default NULL,
PRIMARY KEY (`id`)
) TYPE=MyISAM
Developing POJO Class (Book.java)
Book.java is our POJO class which is to be persisted to the database table "book".
/** |
Add the following mapping code into the contact.hbm.xml file
<class name="roseindia.tutorial.hibernate.Book" table="book"> <id name="lngBookId" type="long" column="id" > <generator class="increment"/> </id> <property name="strBookName"> <column name="bookname" /> </property> </class> |
Write the client program and test it out
Here is the code of our client program to test the application.
/** generator element to |
No comments:
Post a Comment