您的位置:首页 > 编程语言 > Java开发

(10)Spring和Hibernate整合过程中遇到的问题

2016-07-26 14:53 501 查看
1、XX is not valid without active transaction
Spring和Hibernate整合过程中的一个问题就是:
org.hibernate.HibernateException: 方法 is not valid without active transaction
导致这个问题的原因是,在hibernate.cfg.xml文件中有如下配置:
<!-- 配置session的创建方式:线程方式创建session对象 -->
<property name="hibernate.current_session_context_class">thread</property>
解决方法是:只要将这个配置删除或注释掉就可以了。

接下来讲出原因。

在Hibernate当中,有一个很重要的接口是SessionFactory,它提供了获取Session的方法,其中一个方法是
public org.hibernate.classic.Session getCurrentSession()
Obtains the current session. The definition of what exactly "current" means controlled by the org.hibernate.context.CurrentSessionContext impl configured for use.

getCurrentSession()方法的作用是得到current session。然而,“current”代表什么呢?它是由org.hibernate.context.CurrentSessionContext接口的具体实现决定的。

换句话说,Hibernate提供CurrentSessionContext接口来管理“current session”中“current”的含义。
/**
* Defines the contract for implementations which know how to scope the notion
* of a {@link org.hibernate.SessionFactory#getCurrentSession() current session}.
* <p/>
* Implementations should adhere to the following:
* <ul>
* <li>contain a constructor accepting a single argument of type
* {@link org.hibernate.engine.SessionFactoryImplementor}
* <li>should be thread safe
* <li>should be fully serializable
* </ul>
* <p/>
* 实现CurrentSessionContext接口的实例应该负责清理由它们生成的current-session。
* Implementors should be aware that they are also fully responsible for
* cleanup of any generated current-sessions.
* <p/>
* 对于每一个SessionFactory来说,只有一个实现CurrentSessionContext接口的实例
* Note that there will be exactly one instance of the configured
* CurrentSessionContext implementation per {@link org.hibernate.SessionFactory}.
*
* @author Steve Ebersole
*/
public interface CurrentSessionContext extends Serializable {
/**
* Retrieve the current session according to the scoping defined
* by this implementation.
*
* @return The current session.
* @throws HibernateException Typically indicates an issue
* locating or creating the current session.
*/
public org.hibernate.classic.Session currentSession() throws HibernateException;
}


<property name="hibernate.current_session_context_class">thread</property>
其中的hibernate.current_session_context_class也就是代表CurrentSessionContext接口的实现,而thread代表org.hibernate.context.ThreadLocalSessionContext,它实现了CurrentSessionContext 接口。
/**
* A {@link CurrentSessionContext} impl which scopes the notion of current
* session by the current thread of execution.
*/
public class ThreadLocalSessionContext implements CurrentSessionContext {

}


然而Spring也提供了对CurrentSessionContext接口的实现,它就是
org.springframework.orm.hibernate3.SpringSessionContext。

/**
* Implementation of Hibernate 3.1's CurrentSessionContext interface
* that delegates to Spring's SessionFactoryUtils for providing a
* Spring-managed current Session.
*
* <p>Used by Spring's {@link LocalSessionFactoryBean} when told to expose a
* transaction-aware SessionFactory. This is the default as of Spring 2.5.
*
* <p>This CurrentSessionContext implementation can also be specified in custom
* SessionFactory setup through the "hibernate.current_session_context_class"
* property, with the fully qualified name of this class as value.
*/
public class SpringSessionContext implements CurrentSessionContext {

}


As of Hibernate 3.1 there is something called contextual sessions and for that hibernate provides the CurrentSessionContext interface. There are several implementations for this (and thread is short for ThreadLocalSessionContext). Spring has its own implementation of this interface the SpringSessionContext class.

Spring by default sets the property to Springs implementation of the CurrentSessionContext, when this is set to something else (other then JTA) this will break springs ability to manage the hibernate session (and thus the transaction).

http://stackoverflow.com/questions/19875485/using-current-session-context-class-property-hibernate-3-hibernate-4/19894550#19894550

2、Unable to instantiate default tuplizer

tuplizer的意思应该是“元组”。
org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]


When I ran into this error the fix was simple. I was simply missing a setter for a property. Make sure you define matching getters / setters for all your properties.

check the whole stack trace - it will tell you the name of the missing method.
http://stackoverflow.com/questions/8891154/unable-to-instantiate-default-tuplizer-org-hibernate-tuple-entity-pojoentitytup/9954068#9954068

原因是因为没有设置正确的setter属性,从stack trace中可以找到是哪一个setter出现了错误。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring hibernate