内部HSQL数据库抱怨特权

Bar*_*lom 13 java database persistence jpa hsqldb

我正在建立一个独立的Java服务,其中包含一个进程内的HSQL数据库.

persistence.xml中

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
 version="2.0">

 <persistence-unit name="manager">

 <class>tr.silvercar.data.entities.User</class>
 <properties>
 <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />
 <property name="javax.persistence.jdbc.user" value="sa" />
 <property name="javax.persistence.jdbc.password" value="" />
 <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:testdb" />

 <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
 <property name="hibernate.max_fetch_depth" value="3" />

 <!-- cache configuration -->
<!-- 
 <property name="hibernate.ejb.classcache.org.hibernate.ejb.test.Item"
  value="read-write" />
 <property
  name="hibernate.ejb.collectioncache.org.hibernate.ejb.test.Item.distributors"
  value="read-write, RegionName" />
 -->
 </properties>

 </persistence-unit>

</persistence>
Run Code Online (Sandbox Code Playgroud)

  emf = Persistence.createEntityManagerFactory("manager");

  User newUser = new User();
  newUser.setName("Testgebruiker");
  newUser.setCredits(100);

  System.out.println("Inserting user");
  EntityManager em = emf.createEntityManager();
  em.persist(newUser);
  em.close();

  System.out.println("Getting user");
  em = emf.createEntityManager();
  User u = (User) em.createQuery("SELECT u FROM User u").getSingleResult();
  em.close();
  System.out.println(u);
Run Code Online (Sandbox Code Playgroud)

在我看来,由于数据库在内存中,而Hibernate应该生成表,我不需要做任何其他事情.但是,在调用时getSingleResult我得到了异常:

org.hsqldb.HsqlException: user lacks privilege or object not found: USER
Run Code Online (Sandbox Code Playgroud)

fre*_*edt 14

您需要将Hibernate 3.5.6或更高版本与HSQLDB 2.2.x或更高版本一起使用.否则,较旧的Hibernte罐可与HSQLDB 1.8.x一起使用.表的名称不是问题.我已经开发了方言并为这个版本运行了Hibernate测试,但是Pascal比我更了解Hibernate的使用情况,并且在这里帮助了很多人.


Maz*_*aze 13

我遇到了同样的问题,并没有提供任何解决方案(真的)帮助了我(在Stackoverflow上有很多帖子与密切相关),但我终于明白了.因此,我认为我分享了我的发现(对于稍长的帖子感到抱歉):

在我的例子中,我使用MySQL数据库将一些现有的UnitTests转换为HSQLDB,以便可以删除外部依赖项.如果您查看以下描述,这一切看起来都很简单:http://eskatos.wordpress.com/2007/10/15/unit-test-jpa-entities-with-in-memory-database/ 但事实证明有点棘手.

我试验过

  1. 不同的版本(如上所述),
  2. createifexists参数(见:http://hsqldb.org/doc/2.0/guide/dbproperties-chapt.html),
  3. 指定不同的用户凭据(username ="sa",password =""是正确的),
  4. 将update,create和create-drop指定为hibernate.hbm2ddl.auto(参见http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/session-configuration.html),
  5. 使用不同类型的DataSource:c3p0,dbcp,...
  6. ...

但这些都没有真正产生任何影响(错误与bww不同).除了user lacks privilege or object not found错误消息之外,我能得到的信息最多的是:( SQL Error: -20, SQLState: IM001 转换为"驱动程序不支持此功能")更明确地我在日志中发现了这一点: [2012-09-24 14:50:45,796] ERROR main::org.hibernate.engine.jdbc.spi.SqlExceptionHelper.logExceptions(144) | This function is not supported

所以事情显然已被打破.事实证明,最大的问题是两件事的组合:1.我没有正确查看日志输出,因为它实际上包含线索2.错误的注释

关于1:

问题是输出包含大量的行,看起来如下(显然可以忽略,甚至还有H2):

[2012-09-25 10:07:13,453] ERROR main::org.hibernate.tool.hbm2ddl.SchemaExport.perform(426) | HHH000389: Unsuccessful: alter table SimpleSubscription drop constraint FKAD76A00F168752B2
[2012-09-25 10:07:13,453] ERROR main::org.hibernate.tool.hbm2ddl.SchemaExport.perform(427) | user lacks privilege or object not found: PUBLIC.SIMPLESUBSCRIPTION
Hibernate: alter table SimpleSubscriptionChange drop constraint FKB3B8189FFC3506ED
[2012-09-25 10:07:13,453] ERROR main::org.hibernate.tool.hbm2ddl.SchemaExport.perform(426) | HHH000389: Unsuccessful: alter table SimpleSubscriptionChange drop constraint FKB3B8189FFC3506ED
[2012-09-25 10:07:13,453] ERROR main::org.hibernate.tool.hbm2ddl.SchemaExport.perform(427) | user lacks privilege or object not found: PUBLIC.SIMPLESUBSCRIPTIONCHANGE
Hibernate: alter table SimpleSubscriptionChange_languages drop constraint FK5A45F07BFC21A8E6
Run Code Online (Sandbox Code Playgroud)

关于2:

在所有这些行之间隐藏了以下几行,这些行实际上消除了错误:

[2012-09-25 10:07:13,468] ERROR main::org.hibernate.tool.hbm2ddl.SchemaExport.perform(426) | HHH000389: Unsuccessful: create table Rule (id bigint generated by default as identity (start with 1), creationTime timestamp not null, deleted BIT not null, lastUpdateTime timestamp, version integer not null, fetcher varchar(255), hash integer not null, primary key (id), unique (id))
[2012-09-25 10:07:13,468] ERROR main::org.hibernate.tool.hbm2ddl.SchemaExport.perform(427) | a UNIQUE constraint already exists on the set of columns in statement [create table Rule (id bigint generated by default as identity (start with 1), creationTime timestamp not null, deleted BIT not null, lastUpdateTime timestamp, version integer not null, fetcher varchar(255), hash integer not null, primary key (id), unique (id))]
Run Code Online (Sandbox Code Playgroud)

所以问题是定义的BaseEntity有一个错误的注释:

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(nullable = false, unique = true)
private Long id;
Run Code Online (Sandbox Code Playgroud)

该字段已被标识为ID(即作为主键),因此不能具有唯一的注释(并且也是nullable多余的注释).将此更改为:

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
Run Code Online (Sandbox Code Playgroud)

一切正常:-)