我的项目有问题。我的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;
}
}
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>
由于
答案 0 :(得分:1)
如果将ID表字段设置为自动递增,则不应尝试插入id,因为这是由derby自动生成的。
或使用Hibernate生成你的身份
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
答案 1 :(得分:0)
如果您以这种方式定义表格(使用GENERATED BY DEFAULT
或GENERATED 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')");