Adding Autoincrement Field using <generator/> in Hibernate

In  my previous post, We are adding id of the user by using parameter value to the constructor.  In general jsp, servlet code If id field is required to be auto increment, we will select the maximum value of the field and next value will be inserted into the field. Same operation is done by using  “incremental” class property of <generator> in Hibernate.

If we need to implement this funtionality to the previous example. We have to just remove id parameter from the constructor and edit the user id mapping to <generator/>. Changes has been highlighted in below piece of code.

	public User(String u_name, String u_password)


	<id name="u_id" type="integer" column="id">
		<generator class="increment"/>

	User user=new User("Sharma","newpassword");;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 202 other followers

%d bloggers like this: