Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm using Hibernate to access MySQL, and I have a table with an auto-increment primary key.

Everytime I insert a row into 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?

like image 733
Neo Avatar asked Jun 14 '11 15:06

Neo


People also ask

How can we auto generate primary key in Hibernate?

If we want to automatically generate the primary key value, we can add the @GeneratedValue annotation. This can use four generation types: AUTO, IDENTITY, SEQUENCE and TABLE. If we don't explicitly specify a value, the generation type defaults to AUTO.

Can auto increment be a primary key?

Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table. Often this is the primary key field that we would like to be created automatically every time a new record is inserted.

Does primary key auto increment MySQL?

The Auto Increment feature allows you to set the MySQL Auto Increment Primary Key field. This automatically generates a sequence of unique numbers whenever a new row of data is inserted into the table.

How can I get next auto increment number in MySQL?

MySQL has the AUTO_INCREMENT keyword to perform auto-increment. The starting value for AUTO_INCREMENT is 1, which is the default. It will get increment by 1 for each new record. To get the next auto increment id in MySQL, we can use the function last_insert_id() from MySQL or auto_increment with SELECT.


2 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.

like image 160
hvgotcodes Avatar answered Oct 18 '22 19:10

hvgotcodes


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")
@GenericGenerator( name = "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;
    }

It's a bit more complex, but it's the kind of thing you can cut and paste, apart from changing the SEQUENCE name.

like image 21
Mick Sear Avatar answered Oct 18 '22 19:10

Mick Sear