2010-09-27 68 views
13

我正在設置一個獨立的Java服務,並且有一個進程內內存HSQL數據庫。內部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> 

代碼

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); 

這似乎對我來說,因爲數據庫是在內存中,和Hibernate應該產生的表,我就不說了需要做其他事情。然而,在調用getSingleResult我得到異常:

org.hsqldb.HsqlException: user lacks privilege or object not found: USER 
+0

不知道這在內存中發生了什麼...... S可能[fredt](http://stackoverflow.com/users/361828/fredt)會顯示一個很好的解釋。以防萬一,你使用的是什麼版本的HSQLDB? – 2010-09-27 20:42:06

+0

@Pascal我正在使用2.0 – 2010-09-27 21:21:45

+0

也許試試1.8.1.3,只是爲了確保。 – 2010-09-27 21:34:50

回答

14

您需要使用HSQLDB的2.2.x版本或更高版本的使用Hibernate 3.5.6或更高版本,一起。否則,舊版的Hibernte jar可以使用HSQLDB 1.8.x.表的名稱不是問題。 我已經開發了方言併爲這個版本運行Hibernate測試,但是Pascal比我更瞭解Hibernate的用法,並且幫助了很多人。

+0

有趣的是,我不知道這些要求(限制?)在版本上。 +1 – 2010-09-27 23:37:40

+1

在將一個新列添加到與SQL保留字 - 在這種情況下爲'WHEN'的實體之後,得到相同的錯誤後,我遇到了這個問題。實體屬性沒有在問題中列出,但在我的情況下,添加帶有轉義的註釋(反引號)解決了問題:''@Column(name =「'when'」)''。也許列的名字只是太爛了:-) – penfold 2011-09-12 00:15:06

+0

hibernate目前是否支持HSQLDB 2.2.8? – Bas 2012-03-16 09:54:33

0

我也面臨同樣的錯誤。當我在「connection.url」中給出了腳本文件的絕對路徑時,它得到了解決。

<屬性名= 「connection.url」> JDBC:HSQLDB:文件:C:\休眠-例如\數據庫\ MYDB;關機=真< /屬性>

+0

謝謝,但我正在使用存儲在內存中的數據庫,而不是在文件中。 – 2010-09-28 14:56:06

0

當我有這個錯誤,我意識到,我用錯了供應商類persistence.xml

對於休眠應該

<provider>org.hibernate.ejb.HibernatePersistence</provider> 

而對於的EclipseLink應該

<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> 

你還應該注意的是不同的名稱,在使用persistence.xml和創建時Persistence.createEntityManagerFactory("name")

12

我遇到了同樣的問題,並沒有提供任何解決方案(真的)幫助我(和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. 指定不同的用戶憑證(用戶名= 「SA」,密碼=「」是正確的),
  4. 指定更新,創建和創建drop爲hibernate.hbm2ddl.auto(請參閱http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/session-configuration.html),
  5. 使用不同類型的數據源:C3P0,二溴氯丙烷,...
  6. ...

但沒有那些真正做出任何區別的(錯誤BTW不同。)。除了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:

的問題是,輸出蘊含着巨大的,看起來像下面的線量(which can apparently be ignored,甚至還有一個ticket for 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 

關於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))] 

所以這個問題是定義的BaseEntity有一個錯誤的註釋ID:

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

該字段已被識別爲ID(即作爲主鍵),因此不能具有唯一的註釋(並且nullable也是多餘的)。此更改爲:

@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
@Column(name = "id") 
private Long id; 

,一切工作正常:-)

+1

ID的錯誤ID註釋的問題也有助於我的情況。非常感謝分享。對於錯誤的(或多餘的)註釋,SQLserver接口不太嚴格... – 2013-12-16 15:36:15

+0

將GenerationType從AUTO更改爲IDENTITY對我來說很有用 – dalvarezmartinez1 2016-01-31 16:06:26

0

嘗試明確的表/列確定被引用的架構名稱。

我剛剛遇到類似的問題,在我的序列中沒有被識別,然後我意識到在我的Hibernate @SequenceGenerator註釋中,我沒有將模式前綴到序列ref。

call next value for EXAMPLE_SCHEMA.TEST_SEQ 
4

我遇到了類似的問題,但在我的情況下,由於列定義問題發生。我使用MySQL的定義,以這樣的方式

@Id 
@GeneratedValue 
@Column(columnDefinition = "INT(11)") 
private long id; 

這似乎不HSQL支持,我改變了定義的:

@Id 
@GeneratedValue 
@Column(columnDefinition = "INTEGER") 
private long id; 

然後測試再次合作。

+0

Thanks!這解決了我的問題。 – 2014-03-07 06:16:46

+0

謝謝!它爲我工作...其實我用@Column(columnDefinition =「text」) – Darshan 2015-10-14 11:42:16

0

HSQL不使用longtext,而使用LONGVARCHAR類型。改變它解決了我的一個類似的問題,它抱怨所需的特權。