问题
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
回答1:
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;
回答2:
If you define your table this way (either with the GENERATED BY DEFAULT
or GENERATED AS ALWAYS
keywords)
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))
you cannot directly insert anything into id
. You get this error when you try to do so. You insert data into an Identity
field like this:
INSERT INTO CONSULTATION (PATIENT_ID, CONSULTATION_DATE) VALUES ('B2345', '4-8-2016')
This way, the id field is autogenerated for you and incremented when ever you populate a new column.
Note I copied the sql statements above from console in Intellij, to use in a java class, add String concatenation as appropriate:
statement.executeUpdate("INSERT INTO Consultation (PATIENT_ID, CONSULTATION_DATE) VALUES " +
"('2345', '4-8-2016')");
来源:https://stackoverflow.com/questions/33753512/an-attempt-was-made-to-modify-the-identity-column-id