无法使用hibernate插入hsql数据库

Cannot insert into hsql database with hibernate

我的问题是,我尝试将一条记录添加到 HSQL 数据库中,但我做不到。 真的不知道出了什么问题以及如何修复 this.All 这真的让我很烦,因为这只是示例,我不能 运行 这个。

一开始我运行 HSQLDB 服务器使用这个命令:

 java -cp ../lib/hsqldb.jar org.hsqldb.server.Server --database.0 file:mydb --dbname.0 xdb 

一切正常,服务器运行ning。 现在我正在尝试以这种方式在数据库中添加一条记录:

private static void fun() {    

    Configuration configuration;
    configuration = new Configuration().addResource("hibernate.cfg.xml");

    configuration.configure();

    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    Session session = sessionFactory.openSession(); 
    Transaction transaction = session.beginTransaction(); 

    Employee employee = new Employee();  
    employee.setName("Please"); 
    employee.setSurname("work !"); 
    employee.setEmployeeType(EmployeeType.DOCTOR); 
    employee.setSalary(10500); 
    employee.addRoom(23);
    employee.setOfficeLocation("ASDASDAS 4p,rom. 102"); 

    session.save(employee);
    transaction.commit();  
    session.close(); 

} 

这是我的配置:

<hibernate-configuration>
<session-factory> 
     <!-- Database connection settings -->
    <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
    <property name="connection.url">jdbc:hsqldb:hsql://localhost:9001/mydb/xdb</property>
    <property name="connection.username">sa</property>
    <property name="connection.password"></property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">2</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.HSQLDialect</property>

    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>

    <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">update</property>


     <mapping class="com.orm.project.model.Employee"/>
</session-factory>

这里是 Employee.java :

@Entity
@Table(name = "EMPLOYEE")
   public class Employee { 
@Id 
@Column(name = "id") 
@GeneratedValue
   private int id;

@Column(name = "firstName") 
   private String name;   

@Column(name = "lastName")
   private String surname;   

@Column(name = "salary")
   private int salary; 

@Enumerated(EnumType.STRING) 
@Column(name = "employee_type")
   private EmployeeType employeeType;  

@ElementCollection 
@CollectionTable(name="EMPLOYEE") 
@JoinColumn(name="ROOM_ID") 
@Column(name = "assigned_rooms")
   private List<Integer> assignedRooms = new ArrayList<Integer>(); 

@Column(name = "office_location") 
    private String officeLocation; 

    // get + set methods 

最后是错误:

Hibernate: insert into EMPLOYEE (id, employee_type, firstName,     
office_location, salary, lastName) values (default, ?, ?, ?, ?, ?)
org.hibernate.exception.ConstraintViolationException: could not execute     
statementatorg.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:72)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3032)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3558)
at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:98)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:492)
at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:197)
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:181)
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:216)
at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:324)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:715)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:707)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:702)
at com.orm.project.App.fun(App.java:51)
at com.orm.project.App.main(App.java:27)
Caused by: java.sql.SQLIntegrityConstraintViolationException: integrity   
constraint violation: NOT NULL check constraint; SYS_CT_10143 table: 
EMPLOYEE column: EMPLOYEE_ID
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.fetchResult(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.executeUpdate(Unknown Source)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
... 23 more 

我的问题是:

  1. 如何解决这个问题?。
  2. 我正在使用 maven 来构建这个 project.It 可以通过这种方式配置 maven 以在 taget 目录中创建数据库和所有需要的文件吗?我希望所有项目都在根目录中。

非常感谢您的帮助。

此致,

彼得

问题好像出在你的生成策略上。使用序列而不是@GeneratedValue。像这样:

@SequenceGenerator(name="EMPLOYEE_GENERATOR",sequenceName="SEQ_EMPLOYEE",allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="EMPLOYEE_GENERATOR")

希望对您有所帮助!