45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:使用hibernate经常碰到的异常有哪些?

使用hibernate经常碰到的异常有哪些?

2016-08-29 18:28:04 来源:www.45fan.com 【

使用hibernate经常碰到的异常有哪些?

在使用hibernate过程中经常碰到一些异常,有些很常见,经过个人经验和网上的搜集,也总结一下:

一.CGLIB异常

org.springframework.orm.hibernate.HibernateSystemException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of com.anyi.fa.model.FaCard.setCardND; nested exception is net.sf.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of com.anyi.fa.model.FaCard.setCardND
net.sf.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of com.anyi.fa.model.FaCard.setCardND
at net.sf.hibernate.persister.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:220)
at net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:2224)
at net.sf.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:319)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:309)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:138)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:941)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:961)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:59)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:51)
at net.sf.hibernate.persister.EntityPersister.load(EntityPersister.java:413)
at net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:2131)

此中异常比较常见,一般是因为null造成的。例如:一个int映射到数据库中,但从数据库读取时是null值这样就会出现此中异常。

解决方式:如果允许为空的字段做一包装(int-->Integer,long-->Long,double--->Double等),其实这在系统设计时就应该根据实际情况考虑到的。

二 。延迟加载问题

net.sf.hibernate.LazyInitializationException:

Failed to lazily initialize a collection - no session or session was closed

此类问题也是比较常见的,一般是由于采用了延迟加载机制(lazy=true),在session关闭之后又调用延迟加载的数据或方法造成的。

解决方式:

在session关闭之前读取,或调用HIbernate.initalize()方法。

三.BigDecimal属性映射时要指定小数位数。

四.其他就是一些级联更新删除,主外建约束错误,操作主,子表顺序的问题了,一般比较容易解决。

 

 

 

本文地址:http://www.45fan.com/a/question/69474.html
Tags: 经常 一些 hibernate
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部