GMan1973
GMan1973

Reputation: 321

org.hibernate.hql.internal.ast.QuerySyntaxException

I am getting this error:

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

I researched the internet and it says I am not using the class name but the table name. I made sure I am doing this correctly. I am trying to connect to a sql server db.

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>

What am I doing wrong?

Upvotes: 2

Views: 10622

Answers (2)

Z.I.J
Z.I.J

Reputation: 1147

You haven't declared your entity classes in persistence.xml config file:

<property name="hibernate.archive.autodetection" value="class, hbm"/>

Upvotes: 0

SJuan76
SJuan76

Reputation: 24895

You are mixing HQL with JPA classes.

EntityManager is from JPA. JPA's Query will expect that you use JPQL (JPA Query Language), like the prepared query in the entity (SELECT t FROM TblEmployee t)

Now, FROM TblEmployee is HQL (Hibernate Query Language), you should use it when you are not using Hibernate as a JPA provider but directly (using Hibernate classes such as Session).

In short:

If you are including imports from java.persistence, do not add imports from org.hibernate and use JPQL (begins with SELECT).

If you are using Hibernate directly, do not use JPA classes like EntityManager and related. It seems that you can use either JPQL or HQL with the Hibernate queries, though.

Some thought food:

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/

Upvotes: 2

Related Questions