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.)
java mysql hibernate
Gabe Johnson Feb 24 '09 at 16:50 2009-02-24 16:50
source share