试图修改标识列'ID' [英] An attempt was made to modify the identity column ' ID'

查看:153
本文介绍了试图修改标识列'ID'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 >造成者:错误42Z23:试图修改身份
> 'ID'



我的实体:

  package com.springapp.mvc.models; 

import javax.persistence。*;

@Entity
@Table(name =USERS,schema =KK,catalog =)
public class UsersEntity {
private int id;
私人字符串名称;
私人字符串密码;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name =ID)
public int getId(){
返回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;

返回true;
}

@Override
public int hashCode(){
int result = id;
result = 31 * result +(name!= null?name.hashCode():0);
result = 31 * result +(password!= null?password.hashCode():0);
返回结果;


$ / code $ / pre
$ b $ h hibernate 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方言 - >
< property name =dialect> org.hibernate.dialect.DerbyDialect< / property>
<! - < property name =hbm2ddl.auto>更新< / property>
启用Hibernate的自动会话上下文管理 - >
< property name =current_session_context_class>线程< / 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映射DTD 3.0 // EN
http://www.hibernate.org/dtd/hibernate-mapping -3.0.dtd>
< hibernate-mapping>

< class name =com.springapp.mvc.models.AccountEntitytable =ACCOUNTschema =KK>
< id name =idcolumn =ID/>
< property name =namecolumn =NAME/>
< property name =accountprefixcolumn =ACCOUNTPREFIX/>
< property name =accountnumbercolumn =ACCOUNTNUMBER/>
< property name =bankcodecolumn =BANKCODE/>
< property name =useridcolumn =USERID/>
< / class>
< class name =com.springapp.mvc.models.BookEntitytable =BOOKschema =KK>
< id name =idcolumn =ID/>
< property name =titlecolumn =TITLE/>
< property name =descriptioncolumn =DESCRIPTION/>
< property name =useridcolumn =USERID/>
< / class>
< class name =com.springapp.mvc.models.UsersEntitytable =USERSschema =KK>
< id name =idcolumn =ID/>
< property name =namecolumn =NAME/>
< property name =passwordcolumn =PASSWORD/>
< / class>
< / hibernate-mapping>

谢谢

解决方案如果您将ID表字段设置为自动增量,那么您不应该尝试插入一个ID,因为这是由德比自动生成的。



或使用Hibernate来生成你的id

$ p $
@GeneratedValue(strategy = GenerationType.AUTO)
private int ID;


I have problem with my project. My apache derby generate id, in table it works. But in app it doesnt work. In derby I set id autoincrement (start with 1, increment by 1), but i get this error:

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

.

My entity:

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;
    }
}

hibernate 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>

Thanks

解决方案

If you set the ID table field to auto-increment then you should not try to insert an id because this is auto generated by derby.

or use Hibernate to generate you id

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

这篇关于试图修改标识列'ID'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆