【问题标题】:Hibernate and MS SQL Server Identity columnHibernate 和 MS SQL Server 标识列
【发布时间】:2013-06-08 00:46:40
【问题描述】:

我是 Hibernate 的新手,无法使用 Identity 列。当我使用身份作为生成器运行我的 java 程序时,它会在表的身份列中给出错误“...无法插入默认值或空值”。当我使用增量作为生成器时,它会给出“...identity_insert 设置为关闭”的错误。

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

我正在使用以下罐子:

  • hibernate-commons-annotations-4.0.1.Final.jar
  • hibernate-core-4.1.9.Final.jar
  • hibernate-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)
)

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>

【问题讨论】:

    标签: java hibernate hibernate-mapping


    【解决方案1】:

    尝试native 作为生成器类

    <id name="uniqueNumber" type="int" column="Unique_Number">
        <generator class="native"/> <!-- This will pick identity, sequence or hilo based on type -->
    </id>
    

    【讨论】:

    • 嗨,Shazin,
      感谢您的回复。
      我尝试了以下方法,但它们都失败了,并出现以下错误:
      native - DEFAULT 或 NULL 不允许作为显式标识值。 [n/a]
      增量 - 当 IDENTITY_INSERT 设置为 OFF 时,无法在表“Audit_Records”中插入标识列的显式值。
      身份 - DEFAULT 或 NULL 不允许作为显式身份值。
      序列 - “值”附近的语法不正确。
      已分配 - 具有相同标识符值的不同对象已与会话关联。
    【解决方案2】:

    sqljdbc4.jar 的使用表明正在使用 MS SQL Server,但方言设置为 HSQLDialect。将其更改为:

    <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
    

    ... 应该解决这个问题 - 确保 native ID 生成使用与 SQL Server 兼容的值。

    【讨论】:

      猜你喜欢
      • 2011-04-04
      • 2023-03-28
      • 2023-02-18
      • 1970-01-01
      • 1970-01-01
      • 2021-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多