2016-01-15 40 views
0

在我的項目中,我使用HSQLDB作爲數據庫。構建數據庫並添加了兩個條目。現在我正在嘗試使用JPA獲取這些條目。不幸的是,得到以下例外JPA拋出 - 「錯誤:用戶缺少特權或找不到對象:BOOK」

ERROR: user lacks privilege or object not found: BOOK javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not prepare statement at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692) at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1602) at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:492) at com.top.shelf.lib.entityManagers.BookManager.main(BookManager.java:48) Caused by: org.hibernate.exception.SQLGrammarException: could not prepare statement at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:63) at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109) at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:182) at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:148) at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1928) at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1897) at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1875)

這裏是我的persistance.xml文件。

<class>com.top.shelf.lib.entityManager.Book</class> 

    <properties> 
     <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:MyDB" /> 
     <property name="javax.persistence.jdbc.user" value="sa" /> 
     <property name="javax.persistence.jdbc.password" value="" /> 
     <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" /> 
     <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" /> 
    </properties> 
</persistence-unit> 

我的實體類:

@Entity 
@Table(name = "BOOK") 
public class Book { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "ID") 
    private int id; 

    @Column(name = "TITLE") 
    private String title; 

    @Column(name = "AUTHOR") 
    private String author; 

    @Column(name = "BRIEF_DESCRIPTION") 
    private String description; 

    @Column(name = "ISBN") 
    private String isbn; 

    @Column(name = "LOCATION") 
    private String location; 

    @Column(name = "USER_ID") 
    private String user_id; 

    public Book(String title, String author, String description, String isbn, 
      String location) { 
     this.title = title; 
     this.author = author; 
     this.description = description; 
     this.isbn = isbn; 
     this.location = location; 
    } 

    public int getId() { 
     return id; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public String getAuthor() { 
     return author; 
    } 

    public String getDescription() { 
     return description; 
    } 

    public String getIsbn() { 
     return isbn; 
    } 

    public String getLocation() { 
     return location; 
    } 

    public String getUser_id() { 
     return user_id; 
    } 

} 
+0

添加persistence.xml中的 ozgur

回答

1

嘗試:

<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" transaction-type="RESOURCE_LOCAL"> 
     <provider>org.hibernate.ejb.HibernatePersistence</provider> 
     <class>com.top.shelf.lib.entityManager.Book</class> 

     <properties> 
      <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/> 
      <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:MyDB"/> 
      <property name="javax.persistence.jdbc.user" value="sa"/> 
      <property name="javax.persistence.jdbc.password" value=""/> 
      <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/> 
      <property name="hibernate.hbm2ddl.auto" value="update"/> 
     </properties> 
     </persistence-unit> 
    </persistence> 
+0

現在我沒有得到任何異常,但結果設置爲空 –

+0

數據庫是否記錄? – ozgur

+0

是的,表中有兩個條目。但顯示空的結果。 (請參考下面的示例)查詢q = em.createQuery(「SELECT b FROM Book b WHERE b.title =:title」);' 'System.out.println(q.getResultList()。size());' –

相關問題