Hibernate hbm2ddl.auto create属性不工作

Hibernate hbm2ddl.auto create属性不工作,hibernate,jpa,Hibernate,Jpa,我已经创建了演示应用程序,其中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, 2

我已经创建了演示应用程序,其中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>

代码中出现ConstraintViolationException的原因如下:

1: 默认策略为hibernate is
assigned
确保您没有分配重复的id,并且如果您希望hibernate/JPA将为您处理id生成,那么您应该添加@GeneratedValue`注释,它将默认值id自动递增。您可以根据您的要求进行更改

  @Id
        @GeneratedValue
        private int employeeId;
2: 确保您没有向2个不同的对象提供重复的电子邮件地址并保存


3:firstName,lastName不能为空

您可以添加您的员工类代码和hibernate.cfg.xml(hibernate配置)文件,以及您将员工对象保存在何处的代码。@bhushanniyal添加了所需的代码
  @Id
        @GeneratedValue
        private int employeeId;