This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread 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 OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate max function" Watch "Hibernate max function" New topic

Hibernate max function

Simar Suvir

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 | |
Simar Suvir

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.
I agree. Here's the link:
subject: Hibernate max function
It's not a secret anymore!