2012年12月20日星期四

Hibernate中findById时出现org.hibernate.MappingException: Unknown entity:的解决办法

 

原因:在使用myeclispe生成pojo和DAO代码后,对相关类进行了重构,改变了包。造成反射时找不到类。

出现错误的findById代码

public Trace findById(Long id) {
        log.debug("getting Trace instance with id: " + id);
        try {
            Trace instance = (Trace) getHibernateTemplate().get("Trace", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

 

修改后的方法

public Trace findById(Long id) {
        log.debug("getting Trace instance with id: " + id);
        try {
            Trace instance = (Trace) getHibernateTemplate().get("cn.cslg.Trace", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

没有评论:

发表评论