虽然在Hibernate的学习中看到出现的错误时感觉很头大,但是在解决过程中会让人很有成就感,并且学到很多新的知识。将这些错误保存下来,在出现相同错误时能很快的解决它们。对于我们来说,这些错误也是一笔宝贵的财富。
1.The class has no identifier property: entity.Record:类没有标识属性
类没有标识属性,给类加标识属性,检查数据库的联合主键时否设置成功,检查联合主键所对应的.xml文件是否设置成功。
2.org.hibernate.TransactionException: nested transactions not supported:嵌套事务不支持
session.getTransaction().close错写成了session.beginTransaction().close
3.org.hibernate.HibernateException: /cfg.xml not found没有发现Hibernate.cfg.xml配置文件
Configuration cfg = new Configuration().configure();中的路径应该书写正确的Hibernate.cfg.xml的路径,或者在src下没有Hibernate.cfg.xml文件。
4.违反唯一约束条件 (DEV1.RECORDS_PK)
主键在插入时出现重复,认真检查一下自己的测试代码主键是否重复
5.could not execute statement:不能执行语句:
session.getTransaction().close错写成了session.beginTransaction().close,或者同一个session里面有了两个相同标识但是是不同实体
6.a different object with the same identifier value was already associated with the session: [entity.Book#69]:不同对象具有相同的标识符值已经与会话关联起来
错误原因:在hibernate中同一个session里面有了两个相同标识但是是不同实体。
session.evict(bk);清除session缓存中的游离状态的对象,将对象转化为瞬时状态,hibernate不允许有两个相同标识符的对象
7.无根测试
无根测试主要是junit测试方法不对,少写@Test 或者after或before class的方法出错,或者Configuration cfg = new Configuration().configure();中的路径是否书写正确
8.节点遍历不能为空!
通常此类错误都是由于HQL语句写的不正确,总之仔细检查HQL语句,看看有没有语法错误即可。
9.net.sf.hibernate.MappingExcepyion:主要是映射文件出错。
此外xml文件中实体的属性名和列名要书写正确,减少因粗心而犯低级错误。