gpt4 book ai didi

java - 尝试修改标识列 ' ID'

转载 作者:行者123 更新时间:2023-11-30 07:47:12 25 4
gpt4 key购买 nike

我的项目有问题。我的 apache derby 生成 id,在表中它可以工作。但在应用程序中它不起作用。在 Derby 中,我设置了 id 自动增量(从 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;
}
}

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>

映射.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 表字段设置为自动递增,那么您不应尝试插入 ID,因为这是由 derby 自动生成的。

或使用 Hibernate 生成您的 id

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

关于java - 尝试修改标识列 ' ID',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33753512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com