[英]Hibernate and MS SQL Server Identity column
I'm new to Hibernate and am unable to use an Identity column. 我是Hibernate的新手,无法使用Identity列。 When I run my java program using identity as generator, it gives the error that it "...cannot insert default or null value" in the identity column in the table.
当我使用identity作为生成器运行我的java程序时,它会在表中的标识列中给出“...无法插入默认值或空值”的错误。 When I use increment as generator, it gives the error that "...identity_insert is set to off".
当我使用increment作为生成器时,它会给出“... identity_insert设置为off”的错误。
Can someone please guide me how to fix this issue so I can use Hibernate with my table? 有人可以指导我如何解决这个问题所以我可以使用Hibernate与我的表? Please let me know if I need to provide any other information.
如果我需要提供任何其他信息,请告诉我。
I'm using the following jars: 我正在使用以下罐子:
My Table: 我的表:
Create Table ABC (
Unique_Number int IDENTITY(1,1),
Col1 varchar(100),
Col2 char(10),
CONSTRAINT pk_ABC_id PRIMARY KEY(Unique_Number)
)
hbm.xml: 的hbm.xml:
<class name="org.data.ABCData" table="ABC">
<meta attribute="class-description">This is a test class.</meta>
<id name="uniqueNumber" type="int" column="Unique_Number">
<generator class="identity"/> <!-- tried identity, increment -->
</id>
<property name="col1" column="Col1" type="string" length="100"/>
<property name="col2" column="Col2" type="string" length="10"/>
</class>
ABC Element Class: ABC元素类:
public class ABC {
private int uniqueNumber;
private String col1;
private String col2;
public int getUniqueNumber() {
return uniqueNumber;
}
public void setUniqueNumber(int uniqueNumber) {
this.uniqueNumber = uniqueNumber;
}
public int getCol1() {
return col1;
}
public void setCol1(String col1) {
this.col1 = col1;
}
public int getCol2() {
return col2;
}
public void setCol2(String col2) {
this.col2 = col2;
}
}
hibernate.cfg.xml: hibernate.cfg.xml中:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class"></property>
<property name="hibernate.connection.url"></property>
<property name="hibernate.connection.username"></property>
<property name="connection.password"></property>
<property name="connection.pool_size">1</property>
<property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="show_sql">false</property>
<mapping resource="data.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Try native
as generator class 尝试
native
作为生成器类
<id name="uniqueNumber" type="int" column="Unique_Number">
<generator class="native"/> <!-- This will pick identity, sequence or hilo based on type -->
</id>
The use of sqljdbc4.jar
suggests MS SQL Server is being used but the dialect is set to HSQLDialect
. sqljdbc4.jar
的使用表明正在使用MS SQL Server,但方言设置为HSQLDialect
。 Change this to: 将其更改为:
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
... should resolve this - ensuring native
ID generation uses values compatible with SQL Server. ...应解决此问题 - 确保
native
ID生成使用与SQL Server兼容的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.