[英]Hibernate and MS SQL Server Identity column
我是Hibernate的新手,無法使用Identity列。 當我使用identity作為生成器運行我的java程序時,它會在表中的標識列中給出“...無法插入默認值或空值”的錯誤。 當我使用increment作為生成器時,它會給出“... identity_insert設置為off”的錯誤。
有人可以指導我如何解決這個問題所以我可以使用Hibernate與我的表? 如果我需要提供任何其他信息,請告訴我。
我正在使用以下罐子:
我的表:
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:
<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元素類:
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-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>
嘗試native
作為生成器類
<id name="uniqueNumber" type="int" column="Unique_Number">
<generator class="native"/> <!-- This will pick identity, sequence or hilo based on type -->
</id>
sqljdbc4.jar
的使用表明正在使用MS SQL Server,但方言設置為HSQLDialect
。 將其更改為:
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
...應解決此問題 - 確保native
ID生成使用與SQL Server兼容的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.