简体   繁体   English

在Hibernate 4 + Spring 4 + JTA中查找使用HQL或会话过滤器查询的对象的问题

[英]Issues with finding objects queried with HQL or session filter in Hibernate 4 + Spring 4 + JTA

We recently migrated from Hibernate 3.2.3, Spring 3.2.2 to Hibernate 4.3.11 and Spring 4.2.1 on Weblogic 10.3 我们最近从Weblogic 10.3上的Hibernate 3.2.3,Spring 3.2.2迁移到了Hibernate 4.3.11和Spring 4.2.1。

Now in a transaction when hibernate objects are created and added to a hibernate object's persistent collection and later queried with HQL(using hibernateTemplate) or Hibernate's "Query" feature (in the same transaction and before the end of the transaction), Hibernate fails to find the objects that were added. 现在在事务中,当创建休眠对象并将其添加到休眠对象的持久性集合中,然后又使用HQL(使用hibernateTemplate)或Hibernate的“查询”功能(在同一事务中且在事务结束之前)进行查询时,Hibernate无法找到添加的对象。 This worked with Hibernate 3 and Spring 3 before the upgrade, but now fails. 在升级之前,它可以与Hibernate 3和Spring 3一起使用,但是现在失败了。

eg In Psuedo code, say i have a library class with the following property. 例如,在Psuedo代码中,说我有一个具有以下属性的库类。

class Library{
   private Collection<Books> books;
}

In a transaction, i do the following - 在交易中,我执行以下操作-

...
Book book1 = new Book();
book1.setAuthor("Patrick Holt");
library.getBooks().add(book1);

and later on in the same transaction, Hibernate's "Query" is used with a Filter to look for books by the author, like so 然后在同一事务中,将Hibernate的“查询”与过滤器配合使用,以查找作者的书籍,例如

Session s = hibernateTemplate.getSessionFactory().getCurrentSession();
Query q = s.createFilter(library.getBooks(), "where this.author =  :authorName");
q.setParameter("authorName", "Patrick Holt");
List l = q.list();

q.list() in the above example returns 0 results. 上例中的q.list()返回0个结果。 This would return 1 result before the upgrade. 升级前将返回1个结果。 After the upgrade i get 0 results. 升级后,我得到0个结果。

I read about some changes in the currentSessionContext in upgrades to Hibernate 4 and Spring 4, but I'm not sure what needs to change in order for the behavior to be just like before the upgrade, without the code having to change. 我了解了在升级到Hibernate 4和Spring 4时currentSessionContext中的一些更改,但是我不确定在不更改代码的情况下需要进行哪些更改才能使行为像升级之前一样。

Out hibernate objects are defined in hbm.xml files and here's my configuration of the session factory. 休眠对象在hbm.xml文件中定义,这是我对会话工厂的配置。

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>
                <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
                <prop key="hibernate.cache.provider_configuration_file_resource_path">/WEB-INF/ehcache.xml</prop>
                <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
                <!--
                Tried adding the following properties, but it didn't work either
                <prop key="hibernate.transaction.jta.platform">org.hibernate.engine.transaction.jta.platform.internal.WeblogicJtaPlatform</prop>
                <prop key="hibernate.transaction.factory_class">org.hibernate.engine.transaction.internal.jta.JtaTransactionFactory</prop>-->
            </props>
        </property>
        <property name="dataSource" ref="dataSource"/>
        <property name="mappingJarLocations">
            <list>
                <value>/WEB-INF/lib/app-1.0.jar</value>
            </list>
        </property>
     </bean>

Transaction Manager definition: 交易管理器定义:

<bean id="transactionManager" class="org.springframework.transaction.jta.WebLogicJtaTransactionManager"/>

The datasource is made available over jndi. 数据源可通过jndi使用。

Any ideas on what i need to do to fix this issue? 关于解决此问题我需要做什么的任何想法?

The issue i was having is exactly as described in Spring Jira Issue SPR-13848 . 我遇到的问题与Spring Jira Issue SPR-13848中描述的完全一样。 As i understand, it seems like the OpenSessionInViewFilter opens a request scoped session early and there are issues with syncing with a transaction that's started later. 据我了解,似乎OpenSessionInViewFilter提早打开了一个请求范围的会话,并且与稍后启动的事务同步存在问题。

I could not get rid of my OSIV because of the impact that it would cause throughout my application. 我无法摆脱OSIV,因为它会在整个应用程序中造成影响。 However, i found out that hibernate has a property that will accomplish something similar to prevent lazy loading - 但是,我发现休眠状态具有可以完成类似功能以防止延迟加载的属性-

<prop key="hibernate.enable_lazy_load_no_trans">true</prop>

This solved the issue. 这解决了问题。 Adding this property allowed me to remove the OSIV, and hibernate's AUTO flush mode works as expected in a transaction now. 添加此属性使我可以删除OSIV,并且休眠的AUTO刷新模式现在可以在事务中按预期工作。 I also used the CMTTransactionFactory. 我还使用了CMTTransactionFactory。 All my hibernate properties: 我所有的休眠属性:

             <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>
                <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
                <prop key="hibernate.cache.provider_configuration_file_resource_path">/WEB-INF/ehcache.xml</prop>
                <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
                <prop key="hibernate.enable_lazy_load_no_trans">true</prop>
                <prop key="hibernate.transaction.jta.platform">org.hibernate.engine.transaction.jta.platform.internal.WeblogicJtaPlatform</prop>
                <prop key="hibernate.transaction.factory_class">org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory</prop>
            </props>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM