hibernate学习手记(2)

1.javax.persistence.TransactionRequiredException: no transaction is in progress

出现该问题是我没有开启事务,我是在保存之前调用了doWork方法去设置setAutoCommit为true.

解决办法:如果是用到了spring只需要加上@Transactional就可以了,如果使用原始的方式需要调用会话的beginTransaction()方法

 

2.org.hibernate.HibernateException: No CurrentSessionContext configured!

出现该问题是我没有再cfg.xml中配置会员的context.在cfg.xml加入 <property name="hibernate.current_session_context_class">thread</property>就没问题。

hibernate.current_session_context_class 有两个对应的值 

一个是thread 表示本地事务,一个是jta表示全局事务

关于本地事务和全局事务的概念,大家可以百度或者看下这个文章 http://blog.csdn.net/sunitjy/article/details/6585301

 

3.org.hibernate.exception.GenericJDBCException: could not execute statement 

Caused by: java.sql.SQLException: Field 'SID' doesn't have a default value

org.hibernate.AssertionFailure: null id in DBModel.Students entry (don't flush the Session afterQuery an exception occurs)

出现该问题是因为我把 *.hbm.xml中的id配置项中的 generator 的 class属性配置成了native,而数据库对应的字段没有配置成自动递增。

 

posted on 2017-05-15 08:31  Jack·J  阅读(319)  评论(0编辑  收藏  举报

导航