2

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?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
GMan1973
  • 179
  • 3
  • 5
  • 26
  • Not the issue you found, but when using Hibernate (or JPA or other ORM), I would advice stop using DB concepts (v.g., the `Tbl` in `TblEmployee`) – SJuan76 Mar 26 '15 at 23:35
  • There is inconsistency in your persistence.xml as you are declaring both `transaction-type` transactions and `jta-data-source`. I am assuming you want to use JTA transactions so `transaction-type="RESOURCE_LOCAL"` is not required. – wypieprz Mar 27 '15 at 12:03

2 Answers2

2

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/

SJuan76
  • 24,532
  • 6
  • 47
  • 87
  • Thanks so I modified my ejb to return em.createQuery("select t FROM TblEmployee t", TblEmployee.class).getResultList(); – GMan1973 Mar 27 '15 at 00:21
  • 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 – GMan1973 Mar 27 '15 at 00:22
  • 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 (https://docs.jboss.org/hibernate/ogm/4.1/reference/en-US/html/ogm-configuration.html#ogm-configuration-environments-javaee). But if it was the issue I would expect a different exception to be thrown. – SJuan76 Mar 27 '15 at 09:07
0

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

<property name="hibernate.archive.autodetection" value="class, hbm"/>
Z.I.J
  • 1,157
  • 16
  • 36