如果是在web容器中运行hibernate,则在hibernate.cfg.xml中加入这句话:
<property name="hibernate.current_session_context_class">jta</property>
如果是在一个单独的需要进行JDBC连接的java application中运行hibernate,则这样设置:
<property name="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property>
或者简写
<property name="current_session_context_class">thread</property>
public static void main(String[] args) { Configuration conf = new Configuration().configure(); SessionFactory sf = conf.buildSessionFactory(); Session session = sf.getCurrentSession(); session.beginTransaction(); /*........*/ session.getTransaction().commit(); }