【问题标题】:An attempt was made to modify the identity column ' ID'已尝试修改标识列“ID”
【发布时间】:2016-02-18 14:55:19
【问题描述】:

我的项目有问题。我的 apache derby 生成 id,在表中它可以工作。但在应用程序中它不起作用。在 derby 中我设置了 id autoincrement(从 1 开始,递增 1),但是我得到了这个错误:

> Caused by: ERROR 42Z23 : An attempt was made to modify the identity
> column ' ID'

.

我的实体:

package com.springapp.mvc.models;

import javax.persistence.*;

@Entity
@Table(name = "USERS", schema = "KK", catalog = "")
public class UsersEntity {
    private int id;
    private String name;
    private String password;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Basic
    @Column(name = "NAME")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Basic
    @Column(name = "PASSWORD")
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        UsersEntity that = (UsersEntity) o;

        if (id != that.id) return false;
        if (name != null ? !name.equals(that.name) : that.name != null) return false;
        if (password != null ? !password.equals(that.password) : that.password != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (password != null ? password.hashCode() : 0);
        return result;
    }
}

休眠xml:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.url">jdbc:derby://localhost:1527/MyDB</property>
        <property name="connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.DerbyDialect</property>
        <!-- <property name="hbm2ddl.auto">update</property>
Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>
        <mapping resource="mapping.xml"/>
        <mapping class="com.springapp.mvc.models.AccountEntity"/>
        <mapping class="com.springapp.mvc.models.BookEntity"/>
        <mapping class="com.springapp.mvc.models.UsersEntity"/>
    </session-factory>
</hibernate-configuration>

mapping.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

    <class name="com.springapp.mvc.models.AccountEntity" table="ACCOUNT" schema="KK">
        <id name="id" column="ID"/>
        <property name="name" column="NAME"/>
        <property name="accountprefix" column="ACCOUNTPREFIX"/>
        <property name="accountnumber" column="ACCOUNTNUMBER"/>
        <property name="bankcode" column="BANKCODE"/>
        <property name="userid" column="USERID"/>
    </class>
    <class name="com.springapp.mvc.models.BookEntity" table="BOOK" schema="KK">
        <id name="id" column="ID"/>
        <property name="title" column="TITLE"/>
        <property name="description" column="DESCRIPTION"/>
        <property name="userid" column="USERID"/>
    </class>
    <class name="com.springapp.mvc.models.UsersEntity" table="USERS" schema="KK">
        <id name="id" column="ID"/>
        <property name="name" column="NAME"/>
        <property name="password" column="PASSWORD"/>
    </class>
</hibernate-mapping>

谢谢

【问题讨论】:

  • 所以...不要尝试更改 id。

标签: java hibernate generator derby


【解决方案1】:

如果您将 ID 表字段设置为自动递增,则不应尝试插入 id,因为这是由 derby 自动生成的。

或者使用 Hibernate 生成你的 id

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;

【讨论】:

  • 但如果我在 derby 中删除自动增量,它只会覆盖一个用户。当我在会话中保存用户时,我使用 saveOrUpdate 这应该生成 id,但它不会
  • 我看到你把注解放在了getter上面,可能是这样
  • 通过添加到 mapping.xml 中解决:
【解决方案2】:

如果您以这种方式定义表格(使用 GENERATED BY DEFAULTGENERATED AS ALWAYS 关键字)

CREATE TABLE Consultation (id INTEGER PRIMARY KEY NOT NULL GENERATED BY DEFAULT 
    AS IDENTITY (START WITH 1, INCREMENT BY 1),
 Patient_id CHAR(10) NOT NULL REFERENCES PATIENT(CARDNUMBER),
 Consultation_date CHAR(20))

您不能直接在id 中插入任何内容。当您尝试这样做时会收到此错误。您将数据插入到 Identity 字段中,如下所示:

INSERT INTO CONSULTATION (PATIENT_ID, CONSULTATION_DATE) VALUES ('B2345', '4-8-2016')

这样,id 字段会自动为您生成,并在您填充新列时递增。

注意我从 Intellij 的控制台中复制了上面的 sql 语句,用于 java 类,根据需要添加字符串连接:

 statement.executeUpdate("INSERT INTO Consultation (PATIENT_ID, CONSULTATION_DATE) VALUES "  +
                "('2345', '4-8-2016')");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 2018-07-26
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    相关资源
    最近更新 更多