我正在使用Spring 3休眠4和ORM的新功能。所以低于Exception请帮助。
我知道这个例外的问题已经被问到,但在我的情况下,我在尝试这些解决方案后仍然面临这个问题。
以下是我的实体类
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;
}
}
这是我的daoimplementaion课程
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;
}
}
当我尝试访问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
我试图解决它3天但没有运气,
我正在导入正确的实体类javax.persistence.Entity;并在EdcmsEmployee edbmsEmployee中使用HQL&#34;中的实体类名称,其中edbmsEmployee.employeeid =?&#34; (代码在此行之前正常运行,仅在此行上发出异常)
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
在我的Entity类中,我正确使用了所有注释。 那么为什么我得到这个例外&#34; EdbmsEmployee没有映射&#34;。
我搜索了解决方案,发现如果导入错误的Entity类或者我们在HQL中使用了表名,则会出现此异常。 在我的两个是正确的。 我相信在使用这些注释时,无需在xml文件中映射我的实体类。
答案 0 :(得分:8)
请确保添加&#34; packagesToScan&#34;显式为LocalSessionFactoryBean的属性。
<property name="packagesToScan" value="com.hibernate.yourpackage" />
通过上述声明,hibernate将能够使用数据库类
映射您的实体类答案 1 :(得分:0)
同样的事情发生在我身上我不得不在应用程序类的顶部添加@EntityScan(“com。你的实体所包含的东西”)。