I am working on spring 3 hibernate 4 and new to ORM. so getting below Exception please help.
我正在研究spring 3 hibernate 4和ORM的新特性。因此,在异常下面请帮助。
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 -
当我试图访问dao时,我得到了下面的异常
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,
3天以来我一直在努力解决这个问题,但是运气不好。
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)
我正在导入正确的实体类javax.persistence.Entity;在HQL中使用实体类名“来自EdbmsEmployee EdbmsEmployee,其中的edbmsemployeeid =?”(代码在这行之前运行良好,只在这行出现异常)
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".
在我的实体类中,我正确地使用了所有注释。那么为什么我得到这个异常“EdbmsEmployee没有映射”。
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.
我在google上搜索了这个解决方案,发现如果导入错误的实体类或者在HQL中使用了表名,就会出现这个异常。在我的两方面都是正确的。我相信,在使用这些注释时,不需要将我的实体类映射到xml文件中。
2 个解决方案
#1
8
Please make sure to add the "packagesToScan" property explicitly for LocalSessionFactoryBean.
请确保为LocalSessionFactoryBean显式添加“packagesToScan”属性。
<property name="packagesToScan" value="com.hibernate.yourpackage" />
With above declaration hibernate would be able to map your entity class with database class
通过上面的声明,hibernate将能够用数据库类映射您的实体类。
#2
0
Same thing had happened to me i had to add @EntityScan("com.what ever package your entities are in ") on top of the Application Class .
同样的事情发生在我身上,我不得不添加@EntityScan(“com”)。什么包裹你的实体在”)在应用程序类之上。
#1
8
Please make sure to add the "packagesToScan" property explicitly for LocalSessionFactoryBean.
请确保为LocalSessionFactoryBean显式添加“packagesToScan”属性。
<property name="packagesToScan" value="com.hibernate.yourpackage" />
With above declaration hibernate would be able to map your entity class with database class
通过上面的声明,hibernate将能够用数据库类映射您的实体类。
#2
0
Same thing had happened to me i had to add @EntityScan("com.what ever package your entities are in ") on top of the Application Class .
同样的事情发生在我身上,我不得不添加@EntityScan(“com”)。什么包裹你的实体在”)在应用程序类之上。