简体   繁体   English

Hibernate不尊重MySQL auto_increment主键字段

[英]Hibernate not respecting MySQL auto_increment primary key field

I am trying to learn how Hibernate works, and I am running into an almost unacceptable learning curve. 我正在努力学习Hibernate的工作原理,并且我遇到了几乎无法接受的学习曲线。 I can't see how to get Hibernate to respect the auto_increment policy for my objects. 我看不出如何让Hibernate尊重我的对象的auto_increment策略。 Instead, it is overwriting entries in the database with existing IDs, beginning with 1. 相反,它使用现有ID覆盖数据库中的条目,从1开始。

I have a simple Foo object, backed by a MySQL table defined like this: 我有一个简单的Foo对象,由如下定义的MySQL表支持:

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. 我已经确认用SQL手工插入多个Foo对象( insert into Foo values(); )是正确的。

My Java class has the ID specified using annotations like this: 我的Java类使用如下注释指定了ID:

@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) ). 然后我执行一些测试代码,简单地实例化Foo对象并将它们保存到数据库中(使用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). 我已经尝试了@GeneratedValue位的变体(使用所有可能的策略,不考虑括号子句)。 Somebody even suggested leaving off the GeneratedValue entirely. 有人甚至建议完全放弃GeneratedValue Nothing seems to work. 似乎没什么用。

Am I leaving something out? 我要留下什么了? What am I missing? 我错过了什么? Is Hibernate really this hard? Hibernate真的很难吗?

(If anybody has an alternative Java database persistence option, please suggest one. I am making prototypes, not long-lasting mondo-engineered projects.) (如果有人有另一种Java数据库持久性选项,请建议一个。我正在制作原型,而不是长期的mondo工程项目。)

I believe you want GenerationType.IDENTITY . 我相信你想要GenerationType.IDENTITY MySql does not use a table or sequence for generating the Id value. MySql不使用表或序列来生成Id值。

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: 我在一些家伙的网站上使用了一个hibernate.cfg.xml文件,它有这个:

<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. 关于创建ID的各种方法的另外两个答案是正确的。 My original problem's symptom seemed to do with ID generation, but the actual cause was misconfiguration. 我的原始问题的症状似乎与ID生成有关,但实际原因是配置错误。

I think GenerationType.AUTO is right as is <id ...><generator class="native" /></id> 我认为GenerationType.AUTO是正确的,因为<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/ejb3-api/javax/persistence/GenerationType.html

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

I use the following with auto_increment, works perfectly: 我使用以下auto_increment,完美地工作:

@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;
}

You might wish to have a look at: http://hibernatepojoge.sourceforge.net/ 您可能希望查看: http//hibernatepojoge.sourceforge.net/

It claims to create a fully working application (spring, hibernate, junit tests, etc) just by pointing it to a DB. 它声称只需将其指向数据库即可创建一个完全正常运行的应用程序(spring,hibernate,junit tests等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM