用户登录
用户注册

分享至

hibernate in参数过多

  • 作者: 连云新司机
  • 来源: 51数据库
  • 2021-01-07
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.HibernateCallback;import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

看出来了吧,一个时hibernate3一个时hibernate5,版本不一致怎么接受这样的参数。我使用的hibernate版本是4.3.9。所有都改成
import org.springframework.orm.hibernate4.HibernateCallback;import org.springframework.orm.hi




  事务配置的问题;报错信息里面写的很清楚了;
看下你DAO的事务是怎么控制的;贴出代码;
比较合理的实践是在你的SERVICE控制事务;而不是在DAO层;
解决这个问题应该不难。



  1.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 。




  hibernate能自动处理RuntimeException,如果是其他的,则要自己进行处理的
软件
前端设计
程序设计
Java相关