无法在表格中插入标识列的显式值' Common_Registration'当IDENTITY_INSERT设置为OFF时

时间:2014-05-27 17:47:26

标签: hibernate jsp

通用注册表

CREATE TABLE [dbo].[Common_Registration](
[Auto_Id] [int] IDENTITY(1,1) NOT NULL PK,
[CM_ID] [varchar](50) NOT NULL,
[type] [char](1) NOT NULL,
[Name_of_Institution] [varchar](50) NOT NULL,
[sub_type] [varchar](50) NOT NULL,
[Document] [image] NULL,
[code] [image] NULL)

Common_Login

CREATE TABLE [dbo].[Common_Login](
[Login_Id] [int] IDENTITY(1,1) NOT NULL Pk,
[Auto_Id] [int] NOT NULL FK,
[username] [varchar](50) NOT NULL,
[password] [varchar](50) NOT NULL,
[type] [char](1) NOT NULL,
[timestamp] [datetime] NOT NULL)

CommonRegistration Xml文件 自动生成

  

将CommonRegistration数据映射到数据库

  <?xml version="1.0"?>
 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <!-- Generated 27 May, 2014 1:21:55 PM by Hibernate Tools 3.6.0 -->
    <hibernate-mapping>
  <class name="PSPOJO.CommonRegistration" table="Common_Registration" schema="dbo"  
   catalog="PublicServices">
    <id name="autoId" type="int">
        <column name="Auto_Id" />
        <generator class="assigned" />
    </id>
    <property name="cmId" type="string">
        <column name="CM_ID" length="50" not-null="true" />
    </property>
    <property name="type" type="char">
        <column name="type" length="1" not-null="true" />
    </property>
    <property name="nameOfInstitution" type="string">
    <column name="Name_of_Institution" length="50" not-null="true" />
    </property>
    <property name="subType" type="string">
    <column name="sub_type" length="50" not-null="true" />
    </property>
    <property name="document" type="binary">
        <column name="Document" />
    </property>
    <property name="code" type="binary">
        <column name="code" />
    </property>
<set name="commonLogins" table="Common_Login" inverse="true" lazy="true" 
 fetch="select">
        <key>
            <column name="Auto_Id" not-null="true" />
        </key>
        <one-to-many class="PSPOJO.CommonLogin" />
    </set>
</class>
 </hibernate-mapping>

Common_Login 自动生成

  

将CommonLoginData映射到数据库

  <?xml version="1.0"?>
  <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  <!-- Generated 27 May, 2014 1:21:55 PM by Hibernate Tools 3.6.0 -->
  <hibernate-mapping>
  <class name="PSPOJO.CommonLogin" table="Common_Login" schema="dbo"    
  catalog="PublicServices">
    <id name="loginId" type="int">
        <column name="Login_Id" />
        <generator class="assigned" />
    </id>
    <timestamp name="timestamp" column="timestamp" />
    <many-to-one name="commonRegistration" class="PSPOJO.CommonRegistration" 
     fetch="select">
        <column name="Auto_Id" not-null="true" />
    </many-to-one>
    <property name="username" type="string">
        <column name="username" length="50" not-null="true" />
    </property>
    <property name="password" type="string">
        <column name="password" length="50" not-null="true" />
    </property>
    <property name="type" type="char">
        <column name="type" length="1" not-null="true" />
    </property>
    </class>
  </hibernate-mapping>

JAVA代码保存 自动生成

    PSPOJO.CommonRegistration registration= new PSPOJO.CommonRegistration();
    try {
    registration.setCmId("1");
    registration.setType('M');
    registration.setNameOfInstitution(request.getParameter("name"));
    registration.setSubType(request.getParameter("subtype"));
    registration.setDocument(null);
    registration.setCode(null);
    mysession.save(registration);

    CommonLogin cl=new CommonLogin();
    cl.setUsername(request.getParameter("username"));
    cl.setPassword(request.getParameter("password"));
    cl.setType('M');    
    cl.setCommonRegistration(registration);
    registration.getCommonLogins().add(cl);

    mysession.save(cl);
    tx.commit();
    mysession.close();
  

尝试保存时出现此错误

org.hibernate.exception.SQLGrammarException: could not execute statement

Cannot insert explicit value for identity column in table 'Common_Registration' when         IDENTITY_INSERT is set to OFF.

1 个答案:

答案 0 :(得分:0)

您的数据库表具有IDENTITY主键ID,并且已分配您的Hibernate配置。

  • 如果您希望数据库自动管理您的ID而不是替换:

<generator class="identity" />

<generator class="native" />
  • 如果要手动管理ID,请从PK数据库表定义中删除IDENTITY。
相关问题