【问题标题】:JPA throws - "ERROR: user lacks privilege or object not found: BOOK"JPA 抛出 - “错误:用户缺少权限或找不到对象:BOOK”
【发布时间】:2016-04-20 22:02:47
【问题描述】:

在我的项目中,我使用 HSQLDB 作为数据库。构建了数据库并添加了两个条目。现在我正在尝试使用 JPA 获取这些条目。不幸的是,得到以下异常

错误:用户缺少权限或找不到对象:BOOK javax.persistence.PersistenceException:org.hibernate.exception.SQLGrammarException:无法准备语句 在 org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692) 在 org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1602) 在 org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:492) 在 com.top.shelf.lib.entityManagers.BookManager.main(BookManager.java:48) 引起:org.hibernate.exception.SQLGrammarException:无法准备语句 在 org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:63) 在 org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42) 在 org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109) 在 org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:182) 在 org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:148) 在 org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1928) 在 org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1897) 在 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;
    }

}

【问题讨论】:

  • 添加persistence.xml

标签: java spring hibernate jpa hsqldb


【解决方案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>

【讨论】:

  • 现在我没有得到任何异常但结果设置为空
  • 数据库是记录吗?
  • 是的,表中有两个条目。但显示空结果。 Query q = em.createQuery("SELECT b FROM Book b WHERE b.title = :title");System.out.println(q.getResultList().size());
  • 即使对于这个查询 - Query q = em.createQuery("SELECT b FROM Book b"); 结果也是 0。
猜你喜欢
  • 1970-01-01
  • 2016-12-31
  • 1970-01-01
  • 2015-11-06
  • 1970-01-01
  • 1970-01-01
  • 2013-03-06
  • 2017-06-14
  • 2019-05-31
相关资源
最近更新 更多