简体   繁体   English

休眠身份列,但不包含主键

[英]Hibernate Identity column but not primary key

I have one sqlserver 2008 r2 datatable, it has one column autoId int identity(1,1), but it's not the primary key, another column varchar(20) is the one. 我有一个sqlserver 2008 r2数据表,它具有一列autoId int identity(1,1),但它不是主键,另一列varchar(20)是主键。 question is : how do i config the hbm file? 问题是:如何配置hbm文件? bellow is my config file,but it got errors when i try to save one instance. 波纹管是我的配置文件,但是当我尝试保存一个实例时出现错误。 "Cannot insert explicit value for identity column in table 'acct_info' when IDENTITY_INSERT is set to OFF." “当IDENTITY_INSERT设置为OFF时,无法在表'acct_info'中为标识列插入显式值。”

        <property name="autoId" type="int">
            <column name="auto_id" not-null="true" unique="true"  />
        </property>

There can be two reasons , either you don't have sufficient privileges in DB for IDENTITY INSERT or there is mismatch in the mechanism by which you are trying to set an identifier in hibernate and DB layer. 可能有两个原因,要么是您在DB中没有足够的IDENTITY INSERT特权,要么是尝试在休眠和DB层中设置标识符的机制不匹配。

You can have a look at your id generation strategy in hibernate definition file 您可以在休眠定义文件中查看ID生成策略

  1. In DB you can change to Set IDENTITY_INSERT to "ON" 在数据库中,您可以更改为Set IDENTITY_INSERT to "ON"
  2. Pick a different generator class 选择其他发电机类别

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM