org.hibernate.HibernateException:非法尝试将集合与hibernate 4中的两个打开会话相关联

时间:2015-05-12 15:03:57

标签: spring hibernate

我在hibernate中使用JTA事务管理器4.是否有人对此问题有所了解。执行flush时我遇到了错误。

以下是我面临上述问题的代码段。

public void saveOrUpdateEvent(Event event)
{
 event = DomainReferenceMapper.map(event);
    Session session = getSessionFactory().getCurrentSession();
    session.saveOrUpdate(event);
    session.flush();

XML:

 <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
            <prop key="hibernate.show_sql">false</prop>
            <!-- prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop-->

             <!-- Package was changed in the updgration of hibernate -->

            <prop key="hibernate.query.factory_class">org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory</prop>
            <prop key="hibernate.cache.provider_class">org.hibernate.cache.TreeCacheProvider</prop>
            <!-- prop key="hibernate.cache.use_query_cache">true</prop -->
             <!--
             Added for hibernate upgradation
             -->
        <prop key="hibernate.cache.use_second_level_cache">false</prop>

        <!--<prop key="hibernate.enable_lazy_load_no_trans">true</prop> 
            --><prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
             <prop key="hibernate.transaction.jta.platform">org.hibernate.service.jta.platform.internal.WeblogicJtaPlatform</prop>
        </props>
    </property>

1 个答案:

答案 0 :(得分:-1)

public void saveOrUpdateEvent(Event event)
{
    event = DomainReferenceMapper.map(event);
    Session session = null ; 
    Transaction tran = null;
    try {
        session = getSessionFactory().getCurrentSession();        
        tran =  session.beginTransaction();
        session.saveOrUpdate(event);
        session.flush();
        tran.commit();
    } catch(Exception ex ) {
        ex.printstacktrance(); 
    }