Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Object Relational Mapping and the fly likes A problem with Hibernate and MySQL query. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "A problem with Hibernate and MySQL query." Watch "A problem with Hibernate and MySQL query." New topic
Author

A problem with Hibernate and MySQL query.

Vishal Srivastav
Ranch Hand

Joined: Nov 29, 2008
Posts: 46

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.


Vishal Srivastava, Software Engineer (Android), Paradigm Creatives
SCJP 5, SCWCD 5 - http://in.linkedin.com/in/srivastavavishal
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Vishal Srivastav
Ranch Hand

Joined: Nov 29, 2008
Posts: 46

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

Joined: Apr 14, 2004
Posts: 10336

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

Joined: Nov 29, 2008
Posts: 46

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30123
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
Don't get me started about those stupid light bulbs.
 
subject: A problem with Hibernate and MySQL query.
 
Similar Threads
Store Password Field in Encryption Form Using MySQL and Hibernate
Update in master table Child Table problem
Facing problem in Schema export
mysql replication and hibernate
MYSQL 5.0 Connection Time Out