org.hibernate.hql.internal.ast.QuerySyntaxException: EdbmsEmployee is not mapped [from EdbmsEmployee edbmsEmployee where edbmsEmployee.employeeid=?]

Sumit Dhakd picture Sumit Dhakd · Sep 30, 2014 · Viewed 12k times · Source

I am working on spring 3 hibernate 4 and new to ORM. so getting below Exception please help.

I know Question for this exception is already asked but in my case i am still facing the issue after trying those solution.

Below is my Entity class

package com.aviva.qc.hrms.entity;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="EDBMS_EMPLOYEE")
public class EdbmsEmployee implements Serializable{

@Id
@Column(name="EMPLOYEEID", nullable=false)
private String employeeid;

@Column(name="FIRSTNAME", nullable=false)
private String firstname;

@Column(name="LASTNAME", nullable=false)
private String lastname;

@Column(name="BANDID", nullable=false)
private String bandid;

@Column(name="DOJ", nullable=false)
private String doj;

@Column(name="DOB", nullable=false)
private String dob;

@Column(name="FUNCTIONNAME", nullable=false)
private String functionname;

public String getEmployeeid() {
    return employeeid;
}

public void setEmployeeid(String employeeid) {
    this.employeeid = employeeid;
}

public String getFirstname() {
    return firstname;
}

public void setFirstname(String firstname) {
    this.firstname = firstname;
}

public String getLastname() {
    return lastname;
}

public void setLastname(String lastname) {
    this.lastname = lastname;
}

public String getBandid() {
    return bandid;
}

public void setBandid(String bandid) {
    this.bandid = bandid;
}

public String getDoj() {
    return doj;
}

public void setDoj(String doj) {
    this.doj = doj;
}

public String getDob() {
    return dob;
}

public void setDob(String dob) {
    this.dob = dob;
}

public String getFunctionname() {
    return functionname;
}

public void setFunctionname(String functionname) {
    this.functionname = functionname;
}   
}

And here is my daoimplementaion class

  package com.aviva.qc.hrms.daoimpl;

  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Repository;
 import org.springframework.transaction.annotation.Transactional;

 import com.aviva.qc.hrms.dao.EdbmsEmployeeDao;
 import com.aviva.qc.hrms.entity.EdbmsEmployee;

 @Repository("edbmsEmployeeDao")
public class EdbmsEmployeeDaoImpl implements EdbmsEmployeeDao{

private static Logger logger = LoggerFactory.getLogger(EdbmsEmployeeDaoImpl.class);

@Autowired
private SessionFactory sessionFactory;

@Transactional(readOnly=true)
public EdbmsEmployee getEdbmsEmployeeDetails(String employeeid){
    Session session = sessionFactory.getCurrentSession();       
    System.out.println("session session "+session);

    EdbmsEmployee edbmsEmployee = (EdbmsEmployee)session.createQuery("from EdbmsEmployee edbmsEmployee where edbmsEmployee.employeeid=?")
            .setParameter("employeeid",employeeid)
            .uniqueResult();

    if(logger.isDebugEnabled()){

        if(edbmsEmployee==null){
            System.out.println("Employee not Found "+edbmsEmployee);
            logger.trace("Employee not Found "+edbmsEmployee);
        }else{
            System.out.println("Employee Found "+edbmsEmployee);
            logger.trace("Employee Found "+edbmsEmployee);
        }
    }

    return edbmsEmployee;
    }
}

And when i am trying to access dao i am getting below Exception -

  org.hibernate.hql.internal.ast.QuerySyntaxException: EdbmsEmployee is not mapped [from EdbmsEmployee edbmsEmployee where edbmsEmployee.employeeid=?]
at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)
at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)
at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:93)
at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:326)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3252)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3141)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:694)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:550)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:287)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:235)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:248)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:119)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:215)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:193)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1649)
at com.aviva.qc.hrms.daoimpl.EdbmsEmployeeDaoImpl.getEdbmsEmployeeDetails(EdbmsEmployeeDaoImpl.java:29)
at com.aviva.qc.hrms.daoimpl.EdbmsEmployeeDaoImpl$$FastClassByCGLIB$$52572915.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:163)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodI

I am trying to resolve it since 3 days but no luck,

I am importing correct Entity class javax.persistence.Entity; and using Entity class name in HQL "from EdbmsEmployee edbmsEmployee where edbmsEmployee.employeeid=?" (Code is running fine before this line giving exception on this line only)

  session session SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=            []];ActionQueue[insertions=[] updates=[] deletions=[] collectionCreations=[]  collectionRemovals=[] collectionUpdates=[]    unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])
Sep 30, 2014 11:17:49 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet spring threw exception

and in my Entity class i have used all annotation correctly. then why i am getting this Exception "EdbmsEmployee is not mapped".

I have googled the solution and found that this exception is come if we import wrong Entity class or if we've used table name in HQL. In my both is correct. And I believe while working with these annotation no need to map my entity class in xml file.

Answer

sumit picture sumit · Sep 30, 2014

Please make sure to add the "packagesToScan" property explicitly for LocalSessionFactoryBean.

<property name="packagesToScan" value="com.hibernate.yourpackage" />

With above declaration hibernate would be able to map your entity class with database class