当前位置:首页>开发>正文

hibernate常见的几种报错 hibernate 异常 怎么解决

2023-07-30 22:21:56 互联网 未知 开发

 hibernate常见的几种报错 hibernate 异常 怎么解决

hibernate常见的几种报错

?. net.sf.hibernate.MappingException 当出现net.sf.hibernate.MappingException: Error reading resource:…异常时一般是因为映射文件出现错误。 当出现net.sf.hibernate.MappingException: Resource: … not found是因为XML配置文件没找到所致,有可能是放置目录不正确,或者没将其加入hibernate.cfg.xml中。2. net.sf.hibernate.PropertyNotFoundException 当出现net.sf.hibernate.PropertyNotFoundException: Could not find a setter for property name in class …时,原因一般是因为XML映射文件中的属性与对应的Java类中的属性的getter或setter方法不一致。3. org.hibernate.id.IdentifierGenerationException 当出现org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save():异常时,一般是因为元素配置不正确,元素缺少其子元素的配置引起。 解决方案:元素映射了相应数据库表的主键字段,对其子元素,其中class的取值可以为increment、identity、sequence、hilo、native……等,更多的可参考hibernate参考文档,一般取其值为native 。具体可参考2.2.2.1小节。4. a different object with the same identifier value was already associated with the session 当出现a different object with the same identifier value was already associated with the session时,一般是因为在hibernate中同一个session里面有了两个相同标识但是是不同实体。 有如下几种解决方案:(1)使用session.clean(),如果在clean操作后面又进行了saveOrUpdate(object)等改变数据状态的操作,有可能会报出"Found two representations of same collection"异常。(2)使用session.refresh(object),当object不是数据库中已有数据的对象的时候,不能使用session.refresh(object)因为该方法是从hibernate的session中去重新取object,如果session中没有这个对象,则会报错所以当你使用saveOrUpdate(object)之前还需要判断一下。(3)session.merge(object),Hibernate里面自带的方法,推荐使用。5. SQL Grammer Exception,Could not execute JDBC batch update 当出现SQL Grammer Exception,Could not execute JDBC batch update异常时,一般是由如下问题引起:(1)SQL语句中存在语法错误或是传入的数据有误(2)数据库的配置不合法,或者说是配置有误。较容易出现的有数据表的映射文件(,hbm.xml文件)配置有误;Hibernate.cfg.xml文件配置有误(3) 当前的数据库用户权限不足,不能操作数据库。

hibernate 异常 怎么解决

Hibernate异常及错误处理——汇总

一、super.getHibernateTemplate()execute(HibernateCallback)
错误信息:
The method execute(HibernateCallback) in the type HibernateTemplate is not applicable for the arguments (new HibernateCallback >(){}) 发生错误的代码: @Override public List queryForPage() throws Exception {return this.getHibernateTemplate().execute( new HibernateCallback>(){@SuppressWarnings("unchecked")@Overridepublic List doInHibernate(Session session) throws HibernateException { Query query=session.createQuery("from UserInfo") return query.list()}}) } 原来是包引用错误,引入包的时候一定要小心哦,我是把所有的spring、Hibernate包都添加进工程了,引入的时候一定要注意: import org.springframework.orm.hibernate3.HibernateCallbackimport org.springframework.orm.hibernate5.support.HibernateDaoSupport 看出来了吧,一个时hibernate3一个时hibernate5,版本不一致怎么接受这样的参数。我使用的hibernate版本是4.3.9。所有都改成 import org.springframework.orm.hibernate4.HibernateCallbackimport org.springframework.orm.hi

使用hibernate注解多对一关联插入出错

并不是什么session的问题
这个错误的意思就是在执行批量更新的时候期望更新的数据量为>0条的数据,但是现在没有找到任何要更新的数据,所以抛出了异常
看了下lz你的方法,有几点疑问
1. 为什么不给对象设置ID,你的ID并没有设置为自增。。。。(不过这个问题和你的问题无关)
2. 你写了大半天的主从表数据的构造过程,为什么最后去调保存的确是sa对象,这个不是你从表的对象么?你不应该写成 this.usersService.doCreate(stockbill) 来实现级联保存么?            
3. 从表对象中为什么没有任何和主表相关的属性...(这种应该属于错误的定义吧)
4. 感觉保存的信息很蹊跷,因为是批量更新异常,但在你的代码中没有发现有批量更新的部分,所以也不知道你的doCreate到底做了什么,从异常信息上看也没有捕获到足够的信息,所以无法判断。

最新文章