My Dao图层可以正常使用MySql。当我尝试使用内存db(HSQLDB)进行单元测试时,我无法持久保存数据。我的实体定义如下
public class Api {
@Id
@Column(name = "api_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private int apiId;
尝试持久时,不会自动生成ID。它被传递为null。
18:05:52.071 [main] DEBUG org.hibernate.SQL - insert into api (api_id, description, version) values (null, ?, ?)
18:05:52.076 [main] DEBUG o.h.e.jdbc.spi.SqlExceptionHelper - could not prepare statement [insert into api (api_id, description, name) values (null, ?, ?)]
我的hibernate属性是
Properties properties = new Properties();
properties.setProperty(ApiConstants.HIBERNATE_DIALECT, "org.hibernate.dialect.HSQLDialect");
properties.setProperty(ApiConstants.HIBERNATE_DRIVER_CLASS, "org.hsqldb.jdbcDriver");
properties.setProperty(ApiConstants.HIBERNATE_CONN_URL, "jdbc:hsqldb:mem:apicloud");
properties.setProperty(ApiConstants.HIBERNATE_CONN_USER, "sa");
properties.setProperty(ApiConstants.HIBERNATE_CONN_PWD, "");
properties.setProperty("hibernate.archive.autodetection", "class,hbm");
properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
properties.setProperty("hibernate.show_sql", "true");
每次触发单元测试时都会创建模式。 Create语句发布如下。
18:05:51.995 [main] DEBUG org.hibernate.SQL - create table api
(api_id integer generated by default as identity (start with 1),
description varchar(255), version varchar(255), primary key (api_id))
如果我遗失了某些内容,请告诉我。
答案 0 :(得分:0)
当我将HSQLDB版本更新为2.2.9时问题解决了。
https://forum.hibernate.org/viewtopic.php?f=1&t=1043498&p=2490106#p2490106
普拉迪普。