Java Hibernate 不尊重 MySQL auto_increment 主键字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/582526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:31:40  来源:igfitidea点击:

Hibernate not respecting MySQL auto_increment primary key field

javamysqlhibernate

提问by Gabe Johnson

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.

我正在尝试了解 Hibernate 的工作原理,但我遇到了几乎无法接受的学习曲线。我看不到如何让 Hibernate 尊重我的对象的 auto_increment 策略。相反,它使用现有 ID 覆盖数据库中的条目,从 1 开始。

I have a simple Fooobject, 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 ( insert into Foo values();)手动插入多个 Foo 对象是正确的。

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

然后我执行一些简单地实例化 Foo 对象并将它们保存到数据库的测试代码(使用session.save(obj))。好像是用自己的主键序列,从1开始,不看表的键策略。它会覆盖那里的任何内容。

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

我已经尝试了一些变体@GeneratedValue(使用所有可能的策略,去掉括号子句)。有人甚至建议GeneratedValue完全放弃。似乎没有任何效果。

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-engineered 项目。)

采纳答案by Clint

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

我相信你想要GenerationType.IDENTITY。MySql 不使用表或序列来生成 Id 值。

回答by cherouvim

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

回答by Piko

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

回答by Gabe Johnson

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. My original problem's symptomseemed to do with ID generation, but the actual cause was misconfiguration.

关于创建 ID 的各种方法的另外两个答案是正确的。我最初问题的症状似乎与 ID 生成有关,但实际原因是配置错误。

回答by Gabe Johnson

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 测试等)。