我得到了这个错误:
Caused by: javax.ejb.EJBException: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: TblEmployee is not mapped [FROM TblEmployee]
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: TblEmployee` is not mapped我在互联网上搜索了一下,结果显示我使用的不是类名,而是表名。我确保我这样做是正确的。我正在尝试连接到sql server数据库。
JPA:
package com.ray.adtf.jpa;
import java.io.Serializable;
import javax.persistence.*;
import java.sql.Timestamp;
/**
* The persistent class for the tblEmployee database table.
*/
@Entity
@Table(name="tblEmployee")
@NamedQuery(name="TblEmployee.findAll", query="SELECT t FROM TblEmployee t")
public class TblEmployee implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="EmployeeID")
private int employeeID;ejbpackage
com.ray.adtf.ejb;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import com.ray.adtf.jpa.TblEmployee;
import java.util.List;
@Stateless
public class GridMasterBean {
@PersistenceContext
private EntityManager em;
public List<TblEmployee> getDisplayGridList() {
return em.createQuery("select t FROM TblEmployee t", TblEmployee.class).getResultList();
}persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" 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_1_0.xsd">
<persistence-unit name="Test-Persistence" transaction-type="RESOURCE_LOCAL">
<jta-data-source>java:/ProgramHierarchy</jta-data-source>
<class>com.ray.adtf.jpa.TblEmployee</class>
<class>com.ray.adtf.jpa.TblProgram</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
</persistence-unit>
</persistence>我做错了什么?
发布于 2015-03-27 07:39:50
您正在将HQL和JPA类混合在一起。
EntityManager来自JPA。JPA的查询将期望您使用JPQL (JPA Query Language),就像实体(SELECT t FROM TblEmployee t)中的准备好的查询一样
现在,FROM TblEmployee是HQL (Hibernate Query Language),当您不使用Hibernate作为JPA提供者,而是直接使用(使用Hibernate类,如Session)时,应该使用它。
简而言之:
如果包含来自java.persistence的导入,请不要添加来自org.hibernate的导入并使用JPQL (以SELECT开头)。
如果您直接使用Hibernate,请不要使用EntityManager和相关的JPA类。不过,您似乎可以将JPQL或HQL用于Hibernate查询。
一些人认为食物:
http://docs.jboss.org/hibernate/orm/4.2/devguide/en-US/html/ch11.html
http://what-when-how.com/hibernate/querying-with-hql-and-jpa-ql-hibernate/
发布于 2016-04-11 20:03:19
您尚未在persistence.xml配置文件中声明实体类:
<property name="hibernate.archive.autodetection" value="class, hbm"/>https://stackoverflow.com/questions/29290750
复制相似问题