`

hibernate常出现的错误总集

 
阅读更多
问题1:An association from the table refers to an unmapped class
答案:一般都是包名和类名不对,有的是没有大写,这个问题的贴吧:http://www.blogjava.net/jjshcc/archive/2010/05/12/320705.html

问题2:org.hibernate.id.Identifier Generation Exception: ids for this class must be manually assigned before calling save(): com.gz.bean.SystemModule at org.hibernate.id.Assigned.generat(Assigned.java:56)
答案:出现这个错误的原因有可能因为,你的表中有个主键。 但是你插入的(调用save)时那个值是null
你要操作的数据表中的id(即主键)的类型设置成了“自动增长类型”,而在你的
hibernate.cfg.xml中,id的生成方式是assigned,即把主键的生成方式改为native,它的特征是能够根据底层数据库自动选择主键生成方式


问题3:Remember that ordinal parameters are 1-based!
原因是
问题发生的原因是:hql语句里不需要参数,却添加了一个参数,删掉添加参数的语句就可以了!
我的HQL语句:String hql="from Users as u where u.username=‘?’ and u.userpassword=‘?’";
Object[] param=new Object[]{“admin”,“000000”};
this.getHibernateTemplate().find(hql,param);
将HQL语句中''中的?没有解析成占位符,确解析成字符串了。
改成如下形式
String hql="from Users as u where u.username=? and u.userpassword=?";
Object[] param=new Object[]{“admin”,“000000”};
this.getHibernateTemplate().find(hql,param);


问题4:Association references unmapped class:   com.gz.ssh.bean.Positionrights
答案:看与Postionrights相关的类的路路径是不是不对


问题5:当我在页面上删除数据是出现:Could not execute JDBC batch update
答案:把要操作这个表的配置文件的一对多的级联设置一下就行。最好设置成all,或者id的值设置为唯一,或者有的属性不能为空,而保存的时候没有赋值


问题6:could not initialize proxy - no Session
答案:把<many-to-one >中的lazy="false",把这个变量设置一下就行啦。


问题7:ids for this class must be manually assigned before calling save()
原因:因为主键生成的方式是assigned,所以主键的值要自己写入,而在保存的时候,没有给主键赋值
答案:给主键赋值


问题8:No row with the given identifier exis
原因: 在Ostaff类中的配置文件<many-to-one name="ODept" class="com.gz.bean.ODept" fetch="select" lazy="false">
         <column name="DEPTNO" length="16" />
        </many-to-one>没有找到相关联的外键。。。


问题9:Could not execute JDBC batch update
答案一般都是没有设置级联关系


问题10:a different object with the same identifier value was already associated
答案
:在hibernate中同一个session里面有了两个相同标识但是是不同实体。
解决方法一:session.clean()
   PS:如果在clean操作后面又进行了saveOrUpdate(object)等改变数据状态的操   作,有可能会报出"Found two representations of same collection"异常。

解决方法二:session.refresh(object)
  PS:当object不是数据库中已有数据的对象的时候,不能使用session.refresh(object)因为该方法是从hibernate的session中去重新取object,如果session中没有这个对象,则会报错所以当你使用saveOrUpdate(object)之前还需要判断一下。

解决方法三:session.merge(object)
PS:Hibernate里面自带的方法,推荐使用。
2、Found two representations of same collection
错误原因:见1。
解决方法:session.merge(object)
以上两中异常经常出现在一对多映射和多对多映射中




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics