org.hibernate.hql.internal.ast.QuerySyntaxException

10,402

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/

Share:
10,402
GMan1973
Author by

GMan1973

Updated on June 04, 2022

Comments

  • GMan1973
    GMan1973 almost 2 years

    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?

  • GMan1973
    GMan1973 about 9 years
    Thanks so I modified my ejb to return em.createQuery("select t FROM TblEmployee t", TblEmployee.class).getResultList();
  • GMan1973
    GMan1973 about 9 years
    I also took out all references to HQL but the same error. The links are helpful but I was not able to solve my issue
  • SJuan76
    SJuan76 about 9 years
    Your code as a desktop application (no EJB, only JPA) runs just fine (I can provide a link if you want to check for differences). For WildFly, the transaction-type usually is JTA or you have to do additional configuration (docs.jboss.org/hibernate/ogm/4.1/reference/en-US/html/…). But if it was the issue I would expect a different exception to be thrown.