Hibernate常见的问题

  • 问题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>




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值