我创建了hbm2dll的演示应用程序作为create(也尝试使用create-drop) EMployee类将ID作为主键,GenerateType为AUTO。 电子邮件字段是唯一且无效的。
我收到以下异常。
Hibernate: call next value for hibernate_sequence
Hibernate: insert into Employee (EMAIL, FIRST_NAME, LAST_NAME, ID) values (?, ?, ?, ?)
Apr 06, 2017 9:19:57 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 23505, SQLState: 23505
Apr 06, 2017 9:19:57 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
**ERROR: Unique index or primary key violation: "UK_ARDF0F11MFA6TUJS3HFLTHWDV_INDEX_7 ON PUBLIC.EMPLOYEE(EMAIL) VALUES ('demo-user2@mail.com', 8)"; SQL statement:**
insert into Employee (EMAIL, FIRST_NAME, LAST_NAME, ID) values (?, ?, ?, ?) [23505-194]
Apr 06, 2017 9:19:57 AM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
INFO: HHH000010: On release of batch it still contained JDBC statements
Apr 06, 2017 9:19:57 AM org.hibernate.internal.ExceptionMapperStandardImpl mapManagedFlushFailure
ERROR: HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]
的hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.h2.Driver</property>
<property name="hibernate.connection.url">jdbc:h2:tcp://localhost/~/test</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping class="EmployeeEntity"></mapping>
</session-factory>
</hibernate-configuration>
员工CLass
@Entity
@Table(name = "Employee", uniqueConstraints = {
@UniqueConstraint(columnNames = "EMAIL") })
public class EmployeeEntity implements Serializable {
private static final long serialVersionUID = -1798070786993154676L;
@Id
private int employeeId;
@Transient
private Address address;
@Column(name = "EMAIL", unique = true, nullable = false, length = 100)
private String email;
@Column(name = "FIRST_NAME", unique = false, nullable = false, length = 100)
private String firstName;
@Column(name = "LAST_NAME", unique = false, nullable = false, length = 100)
private String lastName;
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
答案 0 :(得分:1)
您的代码中存在以下原因导致ConstraintViolationException:
1:
默认策略是在hibernate中assigned
确保你没有分配重复的id,如果你想让hibernate / JPA为你处理id生成那么你应该添加@GeneratedValue`注释,它默认值为id AUTO增量。您可以根据自己的要求进行更改。
@Id
@GeneratedValue
private int employeeId;
2: 确保您没有向2个不同的对象提供重复的电子邮件地址并保存。
3:firstName,lastName不能为null