1

我的问题是我的单元测试失败了,因为我根本无法访问之前刚刚创建的表。

从控制台的输出中,我可以看到执行了以下 Hibernate 命令。

Hibernate: alter table Server_Node drop constraint FK3621657E1249AF15
Hibernate: alter table Server_Node drop constraint FK3621657E2528B004
Hibernate: drop table EmailAccountSettings if exists
Hibernate: drop table Node if exists
Hibernate: drop table Server if exists
Hibernate: drop table Server_Node if exists
Hibernate: create table EmailAccountSettings (id varchar(255) generated by default as identity (start with 1), description varchar(255), name varchar(255), primary key (id))
Hibernate: create table Node (id bigint generated by default as identity (start with 1), name varchar(255), primary key (id), unique (name))
Hibernate: create table Server (id integer generated by default as identity (start with 1), name varchar(255), primary key (id), unique (name))
Hibernate: create table Server_Node (Server_id integer not null, nodes_id bigint not null, primary key (Server_id, nodes_id))
Hibernate: alter table Server_Node add constraint FK3621657E1249AF15 foreign key (nodes_id) references Node
Hibernate: alter table Server_Node add constraint FK3621657E2528B004 foreign key (Server_id) references Server
Hibernate: insert into Server (id, name) values (default, ?)

如您所见,值被插入到表中Server。但是下一个测试用例试图在表中插入一些东西EmailAccountSettings,这会导致以下错误。

javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: user lacks privilege or object not found: EMAILACCOUNTSETTINGS
  at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1377)
  at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1300)
  at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:273)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  ...

知道桌子有什么问题EmailAccountSettings吗?

我使用 Spring + Hibernate + HSQLDB + JUnit 只是为了概述所涉及的组件。

4

1 回答 1

4

找到了这个问题的原因。再看一下建表语句,突然亮了起来。

Hibernate: create table EmailAccountSettings (id varchar(255) generated by default as identity (start with 1), description varchar(255), name varchar(255), primary key (id))

无法使用 datatype 创建标识列varchar。因此,声明没有成功。但这在运行测试时在控制台中不可见。

更改实体中的数据类型后,一切都按预期工作。

于 2013-01-11T13:24:00.500 回答