5

我正在研究 spring 3 hibernate 4 和 ORM 的新手。所以低于异常请帮助。

我知道已经问过这个例外的问题,但在我的情况下,我在尝试这些解决方案后仍然面临这个问题。

下面是我的实体类

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 时,我得到了 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

我试图从 3 天开始解决它,但没有运气,

我正在导入正确的实体类 javax.persistence.Entity; 并在 HQL 中使用实体类名称“来自 EdbmsEmployee edbmsEmployee where edbmsEmployee.employeeid=?” (代码在此行之前运行良好,仅在此行给出异常)

  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

在我的实体类中,我正确使用了所有注释。那么为什么我会收到此异常“EdbmsEmployee 未映射”。

我搜索了解决方案,发现如果我们导入错误的实体类或者我们在 HQL 中使用了表名,就会出现这个异常。在我的两个都是正确的。而且我相信在使用这些注释时不需要在 xml 文件中映射我的实体类。

4

2 回答 2

8

请确保为 LocalSessionFactoryBean 显式添加“packagesToScan”属性。

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

通过上述声明,hibernate 将能够将您的实体类与数据库类映射

于 2014-09-30T07:38:30.667 回答
0

同样的事情发生在我身上,我不得不在 Application Class 的顶部添加 @EntityScan("com.what ever package your entity are in ") 。

于 2016-06-29T18:44:44.207 回答