自动递增 ID MYSQL 5 的休眠问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1838520/
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
Hibernate problems with Auto Increment ID MYSQL 5
提问by Ryan H
So, I just stood up a Spring Hibernate app and I can't seem to get my mapping file right. I am using MySql 5 and an auto incrementing key. Here is the ID portion of my mapping file.
所以,我刚刚启动了一个 Spring Hibernate 应用程序,但似乎无法正确获取映射文件。我正在使用 MySql 5 和一个自动递增的键。这是我的映射文件的 ID 部分。
<hibernate-mapping>
<class name="org.XXXXXXX.Contact" table="contact">
<id name="id" column="id" type="int" unsaved-value="null">
<generator class="native" />
</id>
Here is the SQL generated
这是生成的SQL
insert into contact (title, first_name, middle_name, last_name, suffix, job_title, dob, passport_number, passport_expiration, employer, dietary_restrictions, secondary_contact_fname, secondary_contact_lname, secondary_contact_mname, secondary_contact_title, secondary_contact_suffix, secondary_contact_job_title, emergency_contact_name, emergency_contact_phone, emergency_contact_notes, is_company) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
插入联系人(title、first_name、middle_name、last_name、后缀、job_title、dob、passport_number、passport_expiration、雇主、饮食限制、secondary_contact_fname、secondary_contact_lname、secondary_contact_mname、secondary_contact_title、secondary_contact_suffix、secondary_contact_job_title、emergency_contact_contact_phone 值、emergency_contact_contact_name、emergency_contact_contact_name、emergency_contact_contact_name、 ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Here is the important part of the stack trace:
这是堆栈跟踪的重要部分:
org.hibernate.AssertionFailure: null id in org.XXXXXXX.Contact entry (don't flush the Session after an exception occurs)
org.hibernate.AssertionFailure:org.XXXXXXX.Contact 条目中的空 ID(发生异常后不要刷新会话)
I have tried setting the unsaved-value to "0" and "-1" and sending them over the wire. Any ideas on what I am doing wrong?
我尝试将未保存的值设置为“0”和“-1”并通过网络发送它们。关于我做错了什么的任何想法?
回答by davidemm
You have to remember that Hibernate is a persistence layer and needs to be able to keep track of where an object is in the database. So when it does an insert, it actually will need to query the auto-increment counter to see what the next ID should be. It then inserts the ID into the object and inserts the object into the database. So for hibernate to do in insert, it uses needs to do a select first (unless you're using some sort of GUID generated by the application). When using mySQL auto-increment, use the "identity" generator.
您必须记住,Hibernate 是一个持久层,需要能够跟踪对象在数据库中的位置。因此,当它执行插入操作时,它实际上需要查询自动递增计数器以查看下一个 ID 应该是什么。然后将 ID 插入到对象中,并将对象插入到数据库中。因此,对于要在插入中执行的休眠,它需要先执行选择(除非您使用的是应用程序生成的某种 GUID)。使用 mySQL 自动增量时,请使用“身份”生成器。
Explanation of the various generators:
各种生成器的说明:
http://www.roseindia.net/hibernate/hibernateidgeneratorelement.shtml
http://www.roseindia.net/hibernate/hibernateidgeneratorelement.shtml
A hibernate XML code snippet:
休眠 XML 代码片段:
<id name="id" type="long" unsaved-value="null" >
<column name="uid" not-null="true"/>
<generator class="identity"/>
</id>
回答by Maruthi
"Increment" generator is not cluster friendly. If some other process inserts into the same table. Next insert from hibernate will fail
“增量”生成器不是集群友好的。如果某个其他进程插入到同一个表中。下一次从休眠中插入将失败