我的问题是,我尝试在HSQL数据库中添加一条记录,但我不能。 真的不知道还有什么不对,如何解决这个问题。所有这一切真的让我感到厌恶,因为这只是一个例子,我无法运行。
在开始时,我使用以下命令运行HSQLDB服务器:
java -cp ../lib/hsqldb.jar org.hsqldb.server.Server --database.0 file:mydb --dbname.0 xdb
Everythink没问题,服务器正在运行。 现在我正在尝试以这种方式在数据库中添加一条记录:
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
我的问题是:
我真的很感激任何帮助。
致以最诚挚的问候,
彼得
答案 0 :(得分:0)
问题似乎是你的发电策略。使用序列而不是@GeneratedValue。像这样:
@SequenceGenerator(name="EMPLOYEE_GENERATOR",sequenceName="SEQ_EMPLOYEE",allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="EMPLOYEE_GENERATOR")
希望它有所帮助!