org.hibernate.hql.internal.ast.QuerySyntaxException:未映射Employees

时间:2016-02-25 00:47:18

标签: jpa-2.0

我们有一个应用程序,通过orm.xml文件完成映射,现在尝试添加注释。是否可以将两者结合起来。

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<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="disc" transaction-type="JTA">
        <jta-data-source>disc.jdbc.DataSource.OLTP</jta-data-source>

        <!--  List of Mapping files -->
        <mapping-file>META-INF/discCommon.orm.xml</mapping-file>
        <mapping-file>META-INF/discTerminology.orm.xml</mapping-file>

        <class>com.model.correspondence.Employees</class>
         <class>com.model.correspondence.AbstractEmployees</class>   
      </persistence-unit>
</persistence> 

在实体类

@Entity
@Table(name = "TEST", schema="company") 
public class Employees extends AbstractEmployees{

private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "EMPLOYEE_ID", unique=true, nullable = false)
    private long employeeId;

    @Column(name = "DESCRIPTION",  nullable = false)
    private String description;

Serivceclass

private static final String SEARCH_FROM_ALL_EMPLOYEES= " select o from " +  Employees.class.getSimpleName()+ " o WHERE " +
    " upper(o.description) LIKE :desc";

Query query = entityManager.createQuery(SEARCH_FROM_ALL_EMPLOYEES);
        query.setParameter("desc", "%" + desc.toUpperCase() + "%");
        @SuppressWarnings("rawtypes")
        List employeeList= query.getResultList();

orm.xml文件正在运行,但注释实体类未被映射。以下是错误

  

引起:org.hibernate.hql.internal.ast.QuerySyntaxException:没有映射Employees [从Employees中选择o o WHERE upper(o.description)LIKE:desc] *       在org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)       在org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)       在org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:93)       在org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:324)       在org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3420)       在org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3309)

由于

1 个答案:

答案 0 :(得分:0)

也许您可以尝试在persistence.xml

中添加此行
<exclude-unlisted-classes>false</exclude-unlisted-classes>

有时,由于我们项目的配置不同,生成的类在路径中没有被JPA(以及您使用的实现)可以自动扫描。

所以,就我而言,我使用该行来告诉JPA实现必须映射我放在persistence.xml中的类,并且不要等到自动扫描。

在这种情况下,persistence.xml的可能性为

<?xml version="1.0" encoding="UTF-8"?>
<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="disc" transaction-type="JTA">
        <jta-data-source>disc.jdbc.DataSource.OLTP</jta-data-source>

        <!--  List of Mapping files -->
        <mapping-file>META-INF/discCommon.orm.xml</mapping-file>
        <mapping-file>META-INF/discTerminology.orm.xml</mapping-file>

        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <class>com.model.correspondence.Employees</class>
        <class>com.model.correspondence.AbstractEmployees</class>   
      </persistence-unit>
</persistence> 

我希望,这对你有用......