File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A problem with Hibernate and MySQL query.

 
Vishal Srivastav
Ranch Hand
Posts: 46
Android Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends,

I am creating a small database driven web application using Hibernate and MySQL.

I have a table created with the first column as PRIMARY KEY and AUTO_INCREMENT constraint.

All I want is to get the last updated / inserted records id from the table using SELECT LAST_INSERT_ID().

I was able to do this by simple JDBC coding, but I am not able to think how to do it using Hibernate as I am new to it.

Is there any other way to do it???

Any small hint or suggestion is awaited...

Thanks All.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need this value after an insert you don't have to do anything, Hibenate will get the value for you when you save the object.

If you need it fomr any other reason, your can use the HQL max function, or you can use a native SQL query.
 
Vishal Srivastav
Ranch Hand
Posts: 46
Android Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I used a service class to insert value into the table... and at the same time tried to retrieve the last id with another class' method.

here are the codes -







And all I am getting is this exception....



Where is the problem??
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The last_insert_id() function is not value HQL, so trying to use it as an HQL query will not work. If you really want to use this you will need to use a native sql query.

But this is an unecessary step, as is your class FindLastIdService. Like I said, Hibernate will return the value of a generated ID as part of its save method, if you use it instead of the saveOrUpdate method.
 
Vishal Srivastav
Ranch Hand
Posts: 46
Android Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Sturrock wrote:The last_insert_id() function is not value HQL, so trying to use it as an HQL query will not work. If you really want to use this you will need to use a native sql query.

But this is an unecessary step, as is your class FindLastIdService. Like I said, Hibernate will return the value of a generated ID as part of its save method, if you use it instead of the saveOrUpdate method.



Thank you sir... Actually I am new to Hibernate and depend a lot on APIs. But now the link Hibernate Api is no more working. It was working 3-4 days earlier.

Can you please tell that that is the API downloadable?? Where can I find it???
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33697
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As that is the #1 hit on Google, I'm thinking they have a system issue. You could use a mirror of the API documentation for now.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic