Hibernate和MS SQL Server标识列

Gag*_*ngh 5 java hibernate hibernate-mapping

我是Hibernate的新手,无法使用Identity列.当我使用identity作为生成器运行我的java程序时,它会在表中的标识列中给出"...无法插入默认值或空值"的错误.当我使用increment作为生成器时,它会给出"... identity_insert设置为off"的错误.

有人可以指导我如何解决这个问题所以我可以使用Hibernate与我的表?如果我需要提供任何其他信息,请告诉我.

我正在使用以下罐子:

  • 休眠公地的注解,4.0.1.Final.jar
  • 休眠核心,4.1.9.Final.jar
  • 冬眠-JPA-2.0-API-1.0.1.Final.jar
  • sqljdbc4.jar

我的表:

Create Table ABC (
    Unique_Number int IDENTITY(1,1),
    Col1 varchar(100),
    Col2 char(10),
    CONSTRAINT pk_ABC_id PRIMARY KEY(Unique_Number)
)
Run Code Online (Sandbox Code Playgroud)

的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>
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

sha*_*zin 1

尝试native作为生成器类

<id name="uniqueNumber" type="int" column="Unique_Number">
    <generator class="native"/> <!-- This will pick identity, sequence or hilo based on type -->
</id>
Run Code Online (Sandbox Code Playgroud)