SQL Hiberate问题,jdbc IDENTITY_INSERT设置为OFF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/452238/
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
Hiberate problems, jdbc IDENTITY_INSERT is set to OFF
提问by James McMahon
I am getting JDBC error when I attempt a commit through hibernate to SQL Server
当我尝试通过休眠提交到 SQL Server 时出现 JDBC 错误
Cannot insert explicit value for identity column in table 'Report' when IDENTITY_INSERT is set to OFF
当 IDENTITY_INSERT 设置为 OFF 时,无法为表“报告”中的标识列插入显式值
I am using mappings generated by netbeans that contain,
我正在使用由 netbeans 生成的映射,其中包含,
<class name="orm.generated.Report" table="Report" schema="dbo" catalog="DatabaseName">
<id name="id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>
Which looks to me like it should be doing the identity insert properly.
在我看来,它应该正确地进行身份插入。
Any idea on how to fix this?
关于如何解决这个问题的任何想法?
EDIT:
Some links to documentation, for posterity,
http://www.hibernate.org/hib_docs/v3/reference/en-US/html/mapping.html#mapping-declaration-id-generator
http://www.roseindia.net/hibernate/hibernateidgeneratorelement.shtml
编辑:
一些文档链接,供后人,
http://www.hibernate.org/hib_docs/v3/reference/en-US/html/mapping.html#mapping-declaration-id-generator
HTTP://www.roseindia .net/hibernate/hibernateidgeneratorelement.shtml
采纳答案by cliff.meyers
You cannot insert into an identity column in SQL Server unless "IDENTITY_INSERT" is set to "ON". Since your generator class is "assigned", Hibernate is assuming that you are setting an explicit value for "id" in Java before saving the object and that Hibernate can directly insert the value into the database. You need to either:
除非“IDENTITY_INSERT”设置为“ON”,否则不能插入到 SQL Server 中的标识列中。由于您的生成器类是“分配的”,Hibernate 假设您在保存对象之前在 Java 中为“id”设置了一个显式值,并且 Hibernate 可以直接将该值插入到数据库中。您需要:
- Pick a different generator class, such as "native"
- Set IDENTITY_INSERT to "ON"
- 选择不同的生成器类,例如“本机”
- 将 IDENTITY_INSERT 设置为“ON”
回答by Nikos Stais
It would be best to use wrapper classes like Integer instead of primitive int.
最好使用像 Integer 这样的包装类而不是原始 int。
Taking your code as an example
以您的代码为例
<class name="orm.generated.Report" table="Report" schema="dbo" catalog="DatabaseName">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="assigned" />
</id>
回答by acdcjunior
Here's something that worked for me. Adapt as needed.
这是对我有用的东西。根据需要进行调整。
@SuppressWarnings("deprecation")
public static void saveWithOverwrittenId(Session session, Object entity) {
String tableName = entity.getClass().getSimpleName();
boolean identityInsertSetToOn = false;
try {
session.connection().createStatement().execute("SET IDENTITY_INSERT "+tableName+" ON");
identityInsertSetToOn = true;
session.beginTransaction();
session.saveOrUpdate(entity);
session.getTransaction().commit();
} catch (SQLException e) {
session.getTransaction().rollback();
throw new RuntimeException(e);
} finally {
if (identityInsertSetToOn) {
try {
session.connection().createStatement().execute("SET IDENTITY_INSERT "+tableName+" OFF");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
In my case, the SQL Server table had the same name as the Entity class. For you, this is probably not true. One solution, then, would be to ask the table name as a parameter.
在我的例子中,SQL Server 表与实体类同名。对你来说,这可能不是真的。那么,一种解决方案是将表名作为参数询问。
回答by Bobby
Change the type of the generator class
更改生成器类的类型
BEFORE
前
<id name="id" type="long">
<column name="Id" />
<generator class="assigned" />
</id>
AFTER
后
<id name="id" type="long">
<column name="Id" />
<generator class="native" />
</id>
Now this will work!
现在这将起作用!
回答by lila
try to change the type of the generator class from 'assigned' to 'identity', it worked for me
尝试将生成器类的类型从“已分配”更改为“身份”,它对我有用