0% found this document useful (0 votes)
589 views

How To Get The Auto-Increment Primary Key Value in MySQL Using Hibernate - Stack Overflow

When inserting a new row into a MySQL table with an auto-increment primary key using Hibernate, the primary key value will be populated on the entity object after it is saved and the session is flushed. You can call getId() on the entity object to retrieve the auto-generated primary key value. For portability, using a sequence generator is recommended as it allows retrieving the ID before the object is persisted.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
589 views

How To Get The Auto-Increment Primary Key Value in MySQL Using Hibernate - Stack Overflow

When inserting a new row into a MySQL table with an auto-increment primary key using Hibernate, the primary key value will be populated on the entity object after it is saved and the session is flushed. You can call getId() on the entity object to retrieve the auto-generated primary key value. For portability, using a sequence generator is recommended as it allows retrieving the ID before the object is persisted.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Welcome to Q&A for professional and enthusiast programmers check out the FAQ!

How to get the auto-increment primary key value in MySQL using Hibernate?

I'm using Hibernate to access MySQL, and I got a table with auto-increment primary key. Everytime I insert new row to the table I don't need to specify the primary key. But after I insert a new row how can I get the relative primary key immediately using hibernate? Or I can just use jdbc to do this? Thanx
mysql hibernate auto-increment

asked Jun 14 '11 at 15:56 littne 27 7 100% accept rate feedback

4 Answers
When you save the hibernate entity, the id property will be populated for you. So if you have MyThing thing = new MyThing(); ... // save the transient instance. dao.save(thing); // after the session flushes, thing.getId() should return the id. I actually almost always do an assertNotNull on the id of a persisted entity in my tests to make sure the save worked.
answered Jun 14 '11 at 16:05 hvgotcodes 27.1k 3 22 62 feedback

Once you're persisted the object, you should be able to call getId() or whatever your @ID column is, so you could return that from your method. You could also invalidate the Hibernate first level cache and fetch it again. However, for portability, you might want to look at using Hibernate with sequence style ID generation. This will ease the transition away from MySQL if you ever need to. Certainly, if you use this style of generator, you'll be able to get the ID immediately, because Hibernate needs to resolve the column value before it persists the object: @Id @GeneratedValue (generator="MY_SEQ")

strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator", parameters = { @Parameter(name = "sequence_name", value = "MY_SEQ"), @Parameter(name = "initial_value", value = "1"), @Parameter(name = "increment_size", value = "10") } ) @Column ( name = "id", nullable = false ) public Long getId () { return this.id; }

Welcome to Q&A for professional and enthusiast programmers check out the FAQ!

It's a bit more complex, but it's the kind of thing you can cut and paste, apart from changing the SEQUENCE name.
answered Jun 14 '11 at 16:05 Mick Sear 302 1 9 feedback

When you are calling a save() method in Hibernate, the object doesn't get written to the database immediately. It occurs either when you try to read from the database (from the same table?) or explicitly call flush(). Until the corresponding record is not inserted into the database table, MySQL would not allocate an id for it. So, the id is available, but not before Hibernate actually inserts the record into the MySQL table.
answered Jun 14 '11 at 16:12 Olaf 2,478 2 13 feedback

If you want, you can get the next primary key independently of an object using: Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession(); Query query = session.createSQLQuery( "select nextval('schemaName.keySequence')" ); Long key = (Long) query.list().get( 0 ); return key;
answered Jun 14 '11 at 18:45 iliaden 401 11 feedback

Not the answer you're looking for? Browse other questions tagged mysql hibernate
auto-increment or ask your own question.

question feed

You might also like