无法使用休眠插入 hsql 数据库
Posted
技术标签:
【中文标题】无法使用休眠插入 hsql 数据库【英文标题】:Cannot insert into hsql database with hibernate 【发布时间】:2015-01-31 01:12:35 【问题描述】:我的问题是,我尝试将一条记录添加到 HSQL 数据库中,但我不能。 真的不知道怎么解决这个问题。所有这一切都让我很恼火,因为这只是一个例子,我无法运行它。
一开始我用这个命令运行 HSQLDB 服务器:
java -cp ../lib/hsqldb.jar org.hsqldb.server.Server --database.0 file:mydb --dbname.0 xdb
一切正常,服务器正在运行。 现在我正在尝试以这种方式在数据库中添加一条记录:
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
我的问题是:
-
如何解决这个问题?
我正在使用 maven 构建这个项目。可以通过这种方式配置 maven 以在 taget 目录中创建数据库和所有需要的文件?我希望将所有项目都放在根目录中。
我真的很感激任何帮助。
最好的问候,
彼得
【问题讨论】:
【参考方案1】:问题似乎出在您的生成策略上。使用序列而不是 @GeneratedValue。像这样:
@SequenceGenerator(name="EMPLOYEE_GENERATOR",sequenceName="SEQ_EMPLOYEE",allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="EMPLOYEE_GENERATOR")
希望对你有帮助!
【讨论】:
不幸的是它没有帮助。仍然是“相同”的错误。 :( 您是否有任何方法可以验证序列是否已在数据库中创建?由于您将属性“hbm2ddl.auto”设置为“update”,因此如果在启动时创建序列失败,日志中应该会出现警告。 我确定不是。如果您在此日志中看到:pastebin.com/frEhJfpc 您也会看到。 好的,看看那个日志,你显然有映射问题。消息 NOT NULL 检查约束; SYS_CT_10093 表:EMPLOYEE 列:EMPLOYEE_ID 声明您的 ID 列称为 EMPLOYEE_ID,但是,您正在映射:@Column(name = "id")
。尝试更改您要映射的列的名称,看看效果如何。
是的,现在一切正常 :) 谢谢 Kikin-Sama :)以上是关于无法使用休眠插入 hsql 数据库的主要内容,如果未能解决你的问题,请参考以下文章
Hibernate 无法访问 phpMyAdmin 插入的数据
在内存数据库中嵌入 HSQL 的 Spring DataSource 和 Linux 上的休眠创建-删除排序