- 问题1:
Error parsing JNDI name []......Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property
这个问题出现是因为我们在使用hibernate的时候并没有使用JNDI作为数据源,但是我们在配置sessionfactory的时候却指定了一个name属性,如下所示:
hibernate认为你指定了一个name为“”的JNDI,他会先去找这个名字的JNDI,结果木有找到,所以报错了,觉解方法就是将name去除。
原文:If you give the org.hibernate.SessionFactory
a name in your configuration, Hibernate will try to bind it to JNDI under that name after it has been built.
- 问题2:
org.hibernate.TransactionException: nested transactions not supported
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:154)
这个问题在于我们将hibernate的事物打开了两次,如下所示:
Transaction tran = session.beginTransaction();//其实这一次不仅获取了事物,也将事物打开了
tran.begin();//再次打开,自然就报错了
你可以这样写
Transaction tran = session.getTransaction();
tran.begin();
- 问题3
org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
hibernate4之后丢弃了config.buildSessionFactory()方法,因为他不利于扩展,对于分布式数据库。提倡传入一个ServiceRegistry对象,这个对象需要指明从那个配置文件中读取相关的属性值(这些值就包括了hibernate.dialetc),因此现在不会在new Configure()的时候就将属性文件读取进去,读取的属性值需要在ServiceRegistry指明。解决方法如下:
config.buildSessionFactory(new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build());
- 问题4
org.hibernate.HibernateException: No CurrentSessionContext configured!
这是因为我们在使用getCurrentSession() 的时候需要配置 <property name="current_session_context_class">thread</property> 这么一个属性。这个属性指明我们获取session的位置,在容器中可以配置成:<property name="current_session_context_class">jta</property>