Hibernate,mysql和Tomee - org.hsqldb.HsqlException:用户缺少未找到的权限或对象

时间:2017-06-29 14:28:55

标签: hibernate jpa

proyect编译但是当我尝试时:

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:无法准备   声明

更新:

根异常是:

  

引起:org.hsqldb.HsqlException:用户缺少权限或对象   未找到: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>

Tomee的Server.xml:

<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"/>

3 个答案:

答案 0 :(得分:0)

尝试更新您的查询

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

AS关键字是多余的,我认为是错误的

答案 1 :(得分: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"

答案 2 :(得分:0)

最后我的问题解决了,包括WEB-INF中的文件resources.xml

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

我不明白为什么这个文件是必要的,因为我已经将池和凭证包含在Tomee的server.xml中。