Hibernate 错误原因总结

1、错误: object references an unsaved transient instance  save the transient instance before flushing
  
 错误原因:
  
  只是生成的对象的id==null,因此,系统以为TraveCode对象存在,而TraveCode.id=null ,所以抛出此异常


 if(job.getWorkCondition().getTraveCode()!=null && job.getWorkCondition().getTraveCode().getId()==null){
  job.getWorkCondition().setTraveCode(null);
 }

2、解决a different object with the same identifier value was already associated with the session错误  

        错误原因: 因为在hibernate中同一个session里面有了两个相同标识但是是不同实体

只需要进行session.clean()操作就可以解决了,但是你在clean操作后面又进行了saveOrUpdate(object)操作,有可能会报出"Found two representations of same collection",

其中这篇文章帮助最大http://opensource.atlassian.com/projects/hibernate/browse/HHH-509

最后 通过session.refresh(object)方法就可以解决了,注意,当object不是数据库中已有数据的对象的时候,不能使用 session.refresh(object)因为refresh是从hibernate的session中去重新取object,

如果session中没有这个对象,则会报错所以当你使用saveOrUpdate(object)之前还需要判断一下

当然这个问题最容易解决的办法还是使用Hibernate里面自带的merge() 方法。不过我始终觉得碰到问题就用这种软件自带的非常用方法(和saveOrUpdate(),save(),update()相比)感觉十分不爽。

3、 Hibernate 中使用  oracle 中的 函数

         可使用

         case when 条件  then 结果1 else  结果2

     public List unassignAccountStatistics(Session ses,IndexPager pager){


           StringBuffer innerHQL = new StringBuffer(" from Company com inner join com.clerkBD cBD join cBD.department dep inner join com.accounts account ");


           innerHQL.append(" where (com.source=5 or com.source is null)");


           innerHQL.append(" group by cBD.id,cBD.name,dep.name");


           Query innerQuery = ses.createQuery("select new map(dep.name as depName,cBD.id as empId,cBD.name as empName,count(com.id)

                as        total,                                        sum(account.state) as  readNum,count(account.applyPublish) as applyNum, 

             sum(case when account.deleted=1 then 1 else 0 end) as delNum ) "  + innerHQL.toString());

 


           Query countQuery = ses.createQuery("select count(*) from Employee emp where emp = some (select bd from Company com inner join

                         com.clerkBD bd where             com.source=5 or com.source is null)");


           HibernateUtil.pagingQuery(pager, innerQuery, countQuery);    


           List innerList = innerQuery.list();  


          return innerList;
         }

 

4、net.sf.hibernate.PropertyNotFoundException: Could not find a getter for sitetitle in class dlog4j.formbean.SiteForm

     错误原因:

         由于 POJO 中的 getXXX() 方法的 名称 和 POJO对应的 XML 文件中对应的 属性 名称不一致

   

5、错误1:org.hibernate.HibernateException: createCriteria is not valid without active transaction
解决:错误行是这样的       Criteria criteria = (persondao.getSession())
根据提示 改成这样
Transaction tx = persondao.getSession().beginTransaction();
Criteria criteria = (persondao.getSession());
……
tx.commit();
原因:Criteria不能脱离事务运行

6、: Exception in thread "main" org.hibernate.MappingException: Repeated column in mapping for entity: lizhx.n11n.test.User column: room_id (should be mapped with insert="false" update="false")
原因:在 XXX.hbm.xml 里有重复的映射
解决:  
<property   name="RoomId"   column="room_id"   type="integer"   not-null="false"   length="11"/>
<many-to-one name="room" column="room_id" class="lizhx.n11n.test.Room" cascade="all" outer-join="false"/>
由于hibernate自动生成和手动添加的映射room_id影射重复导致的错误~
只需要把<property>映射删除即可……

猜你喜欢

转载自cjjwzs.iteye.com/blog/1084687