22

I am trying to learn how Hibernate works, and I am running into an almost unacceptable learning curve. I can't see how to get Hibernate to respect the auto_increment policy for my objects. Instead, it is overwriting entries in the database with existing IDs, beginning with 1.

I have a simple Foo object, backed by a MySQL table defined like this:

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

I have confirmed that inserting multiple Foo objects by hand with SQL (insert into Foo values();) does the right thing.

My Java class has the ID specified using annotations like this:

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

I then execute some test code that simply instantiates Foo objects and saves them to the database (using session.save(obj)). It seems that it uses its own primary key sequence, beginning with one, and does not look at the table's key policy. It overwrites whatever was there.

I have tried variations on the @GeneratedValue bit (using all possible strategies, leaving off the parenthetic clause). Somebody even suggested leaving off the GeneratedValue entirely. Nothing seems to work.

Am I leaving something out? What am I missing? Is Hibernate really this hard?

(If anybody has an alternative Java database persistence option, please suggest one. I am making prototypes, not long-lasting mondo-engineered projects.)

Gabe Johnson
  • 1,393
  • 4
  • 16
  • 28
  • 12
    +1 for "almost unacceptable learning curve" – George Armhold Feb 10 '11 at 00:17
  • Ditto. Hibernate is not as smooth ride as EJB2 was, and lot of this has to do with devs refusing to support stuff than technical reasons. – Zds Sep 08 '11 at 09:16
  • Hibernate 5.x no longer uses IDENTITY when you specify GenerationType.AUTO. Instead it looks for hibernate_sequence table for the next id. – Mohsen Jan 11 '16 at 08:28

5 Answers5

30

I believe you want GenerationType.IDENTITY. MySql does not use a table or sequence for generating the Id value.

Clint
  • 8,988
  • 1
  • 26
  • 40
  • Hi Clint, and thanks for the reply. You are right. But that initially did not solve my problem, because I had a subtle, odious problem underlying everything. I'll post my answer under my original question. – Gabe Johnson Feb 24 '09 at 17:46
  • 4
    Er, I changed my mind, I'll just answer it here. I was using a hibernate.cfg.xml file off some dude's web site, and it had this: create This made the system to re-create my table each time I ran my app. Commenting it out solved the problem. – Gabe Johnson Feb 24 '09 at 17:50
  • Man i had the exact same problem with create, thx for finding it ;p – finpingvin Mar 18 '09 at 14:14
  • If anyone still wondering whether to use GeneratedValue at all? I don't think annotating the ID as @GeneratedValue is a good idea as it takes a lot of hit on the performance. For details refer this [link](https://medium.com/@maqbool.ahmed.mca/spring-data-jpa-vs-data-jdbc-evaluation-b36d8834ead6) – Maqbool Ahmed May 10 '20 at 06:30
5

I wrote this in a comment under the accepted answer, but those aren't shown by default so I'll re-post it as an answer.

I was using a hibernate.cfg.xml file off some dude's web site, and it had this:

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

This made the system to re-create my table each time I ran my app. Commenting it out solved the problem.

The other two answers about the various ways to create IDs are correct. My original problem's symptom seemed to do with ID generation, but the actual cause was misconfiguration.

Gabe Johnson
  • 1,393
  • 4
  • 16
  • 28
  • You can also set that value to 'update' if you only want it to automatically match the database to your settings without dropping the data. The docs: https://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/session-configuration.html#configuration-misc-properties – rjferguson Aug 04 '15 at 23:49
2

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

Picks an appropriate strategy for the 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

cherouvim
  • 31,725
  • 15
  • 104
  • 153
1

I use the following with auto_increment, works perfectly:

@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;
}
Piko
  • 4,132
  • 2
  • 21
  • 13
0

You might wish to have a look at: http://hibernatepojoge.sourceforge.net/

It claims to create a fully working application (spring, hibernate, junit tests, etc) just by pointing it to a DB.