使用Hibernate框架在开发中可能遇到的几个异常(更......)
1、如果session中出现相同oid的两个对象,
1 NonUniqueObjectException:A different object with same identifier value w......
2、托管对象的oid如果在数据表中不存在,
1 StaleStateException:Batch update returned row count from update[0]......
建议:通过持久化对象来直接进行修改操作
3、load方法查询不到数据时,会产生异常
1 ObjectNotFoundException
4、持久化对象关联了一个瞬时对象----
1 org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: cn.it8090.domain.
解决:级联缓存,设置cascade
5、配置错误,mapping-->class---resource
Caused by: org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found : cn.it8090.onetomany.Customer : origin(cn.it8090.onetomany.Customer)
6、实体类的属性中使用了 HashSet<T> hashSet = new HashSet<>(); ArrayList<> list = new ArrayList<>(); 等类似这样没有父类引用指向子类对象的集合,会保如下的错。
{java.lang.ExceptionInInitializerError
Caused by: org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: cn.it8090.manytomany.Student.teachers}
解决:使用父类引用指向子类对象即可。(没有找到具体的原因)
7、运用hibernate框架进行命名检索的时候
{ava.lang.ExceptionInInitializerError Caused by: java.lang.IllegalArgumentException: node to traverse cannot be null!}
解决:检查hql语句是否正确。
8、使用getCurrentSession()获取的与线程绑定的session对象,在事务关闭时,session对象也会close。简单说。不需要手动close。
{org.hibernate.SessionException: Session was already closed}
未完待续。。。。2017-11-23 21:23:17