2017-06-29 72 views
0

的proyect編譯,但是當我嘗試:休眠,MySQL和Tomee - org.hsqldb.HsqlException:用戶缺少特權或找不到對象

SupplierEntity entity = em.find(SupplierEntity.class, 1); 

或者我嘗試:

Query query = em.createNamedQuery("SupplierEntity.loadAll"); 
List<SupplierEntity> entityList = query.getResultList(); 

返回錯誤:

javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not prepare statement

UPDATE:

根的例外是:

Caused by: org.hsqldb.HsqlException: user lacks privilege or object not found: SUPPLIERINFO

我試圖刪除從server.xml中和proyect池(資源)沒有失敗,所以我覺得我的問題與配置相關。


我搜索了其他類似的帖子,我認爲我的代碼很好。任何想法?

我使用Tomee作爲服務器。

實體:

@javax.persistence.Entity 
@javax.persistence.Table(name = "SupplierInfo") 
@javax.persistence.NamedQueries({ 

    // Loading list Of Suppliers 
    @javax.persistence.NamedQuery(name = "SupplierEntity.loadAll", query = "SELECT supplier FROM SupplierEntity AS supplier"), 

}) 
public class SupplierEntity implements Serializable, Comparable<SupplierEntity> 
{ 
    private static final long serialVersionUID = 7847645372201362008L; 

    // ----------- Attribute Definitions ------------ 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name="supplierId", unique=true, insertable=true, updatable=true, nullable=false) 
    private Long supplierId; 

    @javax.persistence.Column(name = "supplierCode", unique = false, nullable = true, insertable = true, updatable = true, length =20) 
    private String supplierCode; 

    @javax.persistence.Column(name = "supplierName", unique = false, nullable = true, insertable = true, updatable = true, length =80) 
    private String supplierName; 

    //Getters and Setters 
} 

表在數據庫:

CREATE TABLE `SupplierInfo` (
    `supplierId` bigint(20) NOT NULL AUTO_INCREMENT, 
    `supplierCode` varchar(20) NOT NULL DEFAULT '', 
    `supplierName` varchar(80) NOT NULL DEFAULT '', 
    PRIMARY KEY (`nirvanaId`) 
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4; 

服務:

@Stateless 
public class SupplierService implements Serializable{ 

@PersistenceContext(unitName = "myapp.supplier") 
EntityManager em; 

    public int test(){ 
    ... 
    ... 
     //SupplierEntity entity = em.find(SupplierEntity.class, 5); 
     Query query = em.createNamedQuery("SupplierEntity.loadAll"); 

     List<SupplierEntity> entityList = query.getResultList(); 

    //catch javax.persistence.NoResultException ex 
    //catch Exception ex --> the exception entry into here. 
    } 

的persistence.xml:

<persistence-unit name="myapp.supplier" transaction-type="JTA"> 
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> 
    <jta-data-source>java:openejb/Resource/supplierPool</jta-data-source> 
    <!--SUPPLIER --> 
    <class>com.myapp.entity.SupplierEntity</class> 

    <properties> 
     <property name="hibernate.hbm2ddl.auto" value="none"/> 
     <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> 
     <property name="hibernate.show_sql" value="false"/> 
     <property name="hibernate.format_sql" value="false"/> 
     <property name="hibernate.jdbc.batch_size" value="25"/> 
     <property name="tomee.jpa.factory.lazy" value="true" /> 
     <!-- <property name="tomee.jpa.cdi" value="false" /> --> 
    </properties> 
</persistence-unit> 

server.xml中Tomee的:

<Resource auth="Container" driverClassName="com.mysql.jdbc.Driver" factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" initialSize="34" maxActive="377" maxIdle="233" minEvictableIdleTimeMillis="55000" minIdle="89" name="jdbc/supplierPool" password="pass" removeAbandoned="true" removeAbandonedTimeout="55" testOnBorrow="true" timeBetweenEvictionRunsMillis="34000" type="javax.sql.DataSource" url="jdbc:mysql://xxx.xxx.xxx.xx:3306/myapp_db" username="user" validationInterval="34000" validationQuery="SELECT 1"/> 
+0

沒有與當前線程關聯的事務 - 如果您的服務或方法沒有@Transactional,則會發生此錯誤 –

+0

謝謝,我添加了@Stateless(我認爲@ Transactional在本例中不是必需的),但現在我有總是錯誤「無法準備聲明」。我編輯了我的帖子和標題。 – josekron

+1

答案是堆棧跟蹤的根本原因。你讀過它嗎? – BalusC

回答

0

最後我的問題解決了,包括文件資源。 xml in WEB-INF

<resources> 
    <Resource id="nirvanaDatasource" type="DataSource"> 
     JdbcDriver com.mysql.jdbc.Driver 
     JdbcUrl jdbc:mysql://xxx.xxx.xxx.xx:3306/myapp 
     UserName username 
     Password password 
    </Resource> 
</resources> 

我不明白爲什麼這個fi le是必要的,因爲我已經在Tomee的server.xml中包含了池和證書。

0

嘗試更新查詢

@NamedQuery(name = "SupplierEntity.loadAll", query = "SELECT supplier FROM SupplierEntity supplier") 

AS關鍵字是多餘的,我認爲,錯

+0

我認爲這沒有錯。我的查詢告訴「從SupplierEntity表中獲取完整對象作爲供應商」。如果我只需要一個字段,那麼我可以執行「SELECT supplier.supplierName FROM SupplierEntity as supplier」。 但是我嘗試了你的解決方案,但不工作 – josekron

+0

'AS'關鍵字出現在JPA規範和任何正派的JPQL文檔中,並且很容易檢查。與我對@Nikolay Yashchenko表格名稱 –

0

例外是org.hibernate.exception.SQLGrammarException,我們可以從中瞭解到錯誤來自您的SQL查詢。

當我們來看看你的SQL查詢:

"SELECT supplier FROM SupplierEntity AS supplier" 

我們可以理解,你指的是一列supplier不會在你的表存在,或者你想你的別名表supplierEntity作爲supplier

如果您走樣supplierEntity你應該刪除AS關鍵字,這樣只需使用:

"SELECT supplier FROM SupplierEntity supplier" 

或者,你可以寫:

query = "from SupplierEntity supplier" 
+0

抱怨無關,我嘗試了沒有AS,但我認爲這不是問題。也許它與根本原因有關:「java.sql.SQLSyntaxErrorException:用戶缺少權限或找不到對象:SUPPLIERINFO」。但我已經有一個表「SupplierInfo」 – josekron

+0

另外我也有同樣的問題與em.find() – josekron

+0

如果你有同樣的問題em.find()我認爲這意味着你的數據庫真的沒有一個表名稱爲「supplierinfo」。 –

相關問題