常见hibernate问题

来源:互联网 发布:dxomark 知乎 编辑:程序博客网 时间:2024/06/05 09:40
 
hibernate问题
1.Exception in thread "main" java.lang.NoClassDefFoundError: net/sf/ehcache/CacheException
项目classpath中缺少ehcache.jar
--------------------------------------------------------------------------
2.Exception in thread "main" java.lang.NoClassDefFoundError: org/dom4j/DocumentException
缺少dom4j包
-----------------------------------------------------------------------------------------------
3.Exception in thread "main" java.lang.NoClassDefFoundError: net/sf/cglib/core/KeyFactory
缺少cglib-2.1.jar
-----------------------------------------------------------------------
4.Exception in thread "main" java.lang.NoClassDefFoundError: org/objectweb/asm/Type
缺少asm.jar
----------------------------------------------------------------------------------
5.Exception in thread "main" org.hibernate.HibernateException: Not able to obtain connection
用完之后session关闭,session.isC ,遇到这样的错误要先检查看session
-------------------------------------------------------------------------------------------------
6.%%%% Error Creating SessionFactory %%%%
org.hibernate.MappingException: Could not determine type for: String, for columns: [org.hibernate.mapping.Column(sharefor)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:265)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:252)
at org.hibernate.mapping.Property.isValid(Property.java:175)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:330)
at org.hibernate.mapping.RootClass.validate(RootClass.java:191)
at org.hibernate.cfg.Configuration.validate(Configuration.java:815)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:976)
at com.likeyeejee.hibernate.HibernateSessionFactory.currentSession(HibernateSessionFactory.java:51)
at com.likeyeejee.db.dao.UserDAO.getUserList(UserDAO.java:20)
at com.likeyeejee.db.dao.UserDAO.main(UserDAO.java:41)
Exception in thread "main" java.lang.NullPointerException
at com.likeyeejee.hibernate.HibernateSessionFactory.currentSession(HibernateSessionFactory.java:59)
at com.likeyeejee.db.dao.UserDAO.getUserList(UserDAO.java:20)
at com.likeyeejee.db.dao.UserDAO.main(UserDAO.java:41)

mapping 错误,..hbm.xml中有数据表中没有的字段,例如这里的sharefor字段在该.hbm.xml中有,但对应的数据表中没有sharefor字段.
7.Hibernate中的 NonUniqueObjectException 及其解决之道
NonUniqueObjectException错误,解决如下:org.hibernate.NonUniqueObjectException: 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
在现有的Session中已经绑定了一个对象,而这时,你还要强行将已经持久化的对象再重新加入到Session中。 在一个集合中的对象,随着一对多的中一的Load而级联Load进来,这时你的另一个持久化过的对象又想利用update(),会报这个错误。 又或者当你用session.lock()时,也可能。 总而言之,在你的Session中,已经存在(你想要更新的)对象了。
解决a different object with the same identifier value was already associated with the session错误
实际上在Hibernate中对数据的更新,应该先查询出该记录,然后修改该记录,最后使用Session更新该记录。
或者解决的办法是使用Hibernate里面自带的merge()方法,这种错误经常出现在一对多映射和多对多映射.

No row with the given identifier exists
表示你现在查询的对象所关联的对象有问题,一般是因为数据的问题(该对象所关联的对象找不到)
Caused by: org.dom4j.DocumentException: Invalid byte 2 of 2-byte UTF-8 sequence. Nested exception: Invalid byte 2 of 2-byte UTF-8 sequence.
如果出现这行错误说明你的xml配置文件有不规范的字符,检查下。
net.sf.hibernate.MappingException: Error reading resource: hibernate/Hello_Bean.hbm.xml
如果出现这行错误说明你的hibernate的XML配置文件有错
net.sf.hibernate.MappingException: Resource: hibernate/Hello_Bean.hbm.xml not found
如果出现这行错误说明hibernate的XML配置文件没有找到,你应该把XML文件放在与你的类文件同个目录下,本文中是放在hibernateclasseshibernate目录下,也就是跟Hello_Bean.class类文件一起。
net.sf.hibernate.PropertyNotFoundException: Could not find a setter for property name in class hibernate.Hello_Bean
如果出现这行错误说明你的xml文件里设置的字段名name的值与Hello_Bean.java类里的getXXX或setXXX方法不一致。
net.sf.hibernate.HibernateException: JDBC Driver class not found: org.gjt.mm.mysql.Driver
如果出现这行错误说明你的MYSQL驱动没有加进JB库里或者不在CLASSPATH里。
原创粉丝点击