Hibernate does not honor MySQL auto_increment primary key field

I'm trying to find out how Hibernate works, and I come across an almost unacceptable learning curve. I do not see how to make Hibernate respect the auto_increment policy for my objects. Instead, it overwrites the entries in the database with existing identifiers starting at 1.

I have a simple Foo object supported by a MySQL table defined as follows:

 CREATE TABLE `Foo` ( `fooId` int(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`fooId`), ) 

I confirmed that manually inserting multiple Foo objects using SQL ( insert into Foo values(); ) does the right thing.

My Java class has an identifier specified using annotations such as:

 @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name="fooId") private Integer id; 

Then I run some test code that simply creates the Foo objects and stores them in the database (using session.save(obj) ). It seems that he uses his own sequence of primary keys, starting with one, and does not look at the policy of keywords. He rewrites everything that was there.

I tried the options on the @GeneratedValue bit (using all possible strategies, leaving the header part). Someone even suggested completely abandon GeneratedValue . Nothing is working.

Am I leaving something? What am I missing? Is hibernate really hard?

(If anyone has an alternative way to save a Java database, suggest one of them. I am making prototypes, not long-term projects constructed using mondo.)

+18
java mysql hibernate
Feb 24 '09 at 16:50
source share
5 answers

I believe you want GenerationType.IDENTITY . MySql does not use a table or sequence to generate an Id value.

+28
Feb 24 '09 at 17:06
source share

I wrote this in a comment according to the accepted answer, but they are not displayed by default, so I will resubmit it as an answer.

I used the hibernate.cfg.xml file from the dude website and it had this:

 <property name="hibernate.hbm2ddl.auto">create</property> 

This made the system recreate my table every time I started my application. Commenting on this, the problem is solved.

The other two answers about different ways of creating identifiers are correct. My original problem Symptom seemed to do with the generation of the identifier, but the actual reason was the wrong configuration.

+5
Mar 03 '09 at 15:51
source share

I think GenerationType.AUTO is right, as is <id ...> <generator class = "native" /> </id>

Choosing the appropriate strategy for a particular database.

http://www.hibernate.org/hib_docs/ejb3-api/javax/persistence/GenerationType.html

http://www.hibernate.org/hib_docs/reference/en/html/mapping.html

+2
Mar 02 '09 at 16:28
source share

I use the following with auto_increment, it works fine:

 @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "db_id", unique = true, nullable = false) public Long getDbId() { return this.dbId; } public void setDbId(Long dbId) { this.dbId = dbId; } 
+1
Mar 03 '09 at 0:26
source share

You might want to check out: http://hibernatepojoge.sourceforge.net/

He claims to be creating a fully functional application (spring, hibernate, junit tests, etc.) by simply pointing it to the database.

0
Mar 17 '09 at 15:58
source share



All Articles