【发布时间】:2017-06-29 14:28:55
【问题描述】:
项目编译但是当我尝试时:
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: 用户缺少权限或对象 未找到:供应商信息
我试图从 server.xml 中删除池(资源)并且项目没有失败,所以我认为我的问题与配置有关。
我搜索了其他类似的帖子,我认为我的代码很好。有什么想法吗?
我使用 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"/>
【问题讨论】:
-
没有与当前线程关联的事务 - 如果您的服务或方法上没有 @Transactional 可能会发生此错误
-
谢谢,我添加了@Stateless(我认为在这种情况下@Transactional 不是必需的)但现在我总是出现错误“无法准备语句”。我编辑了我的帖子和标题。
-
答案在于堆栈跟踪的根本原因。你读了吗?
-
"SELECT supplier FROM SupplierEntity AS supplier",您的表中没有suplier列。 -
@BalusC 根本原因是:“java.sql.SQLSyntaxErrorException:用户缺少权限或找不到对象:SUPPLIERINFO”但我已经有一个表“SupplierInfo”