This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Object Relational Mapping and the fly likes Hibernate max function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate max function" Watch "Hibernate max function" New topic
Author

Hibernate max function

Simar Suvir
Greenhorn

Joined: Jul 29, 2011
Posts: 11

There is a need to get the correct id value from the database(sql express 2005) after an insert,update,delete function.This value should exactly reflect in the database once insert or update or delete functions are carried out.for eg.; if I have 5 records in the database and I do a insert it should reflect 6th row with values. On the other hand if I am deleting the record(6th row) and again try to insert values it should get inserted for 6th row only and not for the 7th row(as it happens now).

Kindly help.
Komal Renu
Ranch Hand

Joined: Jul 21, 2011
Posts: 51
What is the Identity specification that you have given for the DB table ? You need to change it from Auto Generated to an insertable Identity(in your hbm or model class).

Now from your java code before you do any operation, you can get the max id for this table from DB, increment it by one and then set it explicitly in your Java code. In this way you control the ID column value from the code instead of relying on the SQl server.

Thanks and Regards,
-------------------------------------------------------------------------------------
Komal Renu | krenu@infocepts.com | www.infocepts.com
-------------------------------------------------------------------------------------
Simar Suvir
Greenhorn

Joined: Jul 29, 2011
Posts: 11

We are using select ident_current('table_name') to get the max id. The id column is identity column. now if i delete the 5th id then it should delete the 5th id in sql server and send us the 6th row as the id.but it is sending us 5th id only.Since it is an identity column we expect server to keep the id and increment accordingly.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Hibernate max function