您好我希望得到以下问题的一些意见。我是Hibernate的新手,并试图将这个难题拼凑在一起。
问题:我的数据库中的数据每天更新一次。我想让我的实体与此同步并刷新它们。我实现了一个Spring / Quartz计时器来调用我的hibernate实现类去刷新实体。我试图确保每次调用此方法时它会清除所有当前实体,以便可以刷新它们。对此最好的方法是什么?
使用Hibernate 3.2与Spring集成。
建议的解决方案:
我尝试使用Session.flush命令和SesionFactory清除但它不起作用。
Spring / Hibernate配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!-- Defines the hibernate session factory to be used by the hibernate support dao classes -->
<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
<property name="dataSource" ref="webDataSrc" />
<property name="annotatedClasses">
<list>
<value>test.foo</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="debug">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">false</prop>
</props>
</property>
</bean>
<!-- get the datasource from the context -->
<bean id="webDataSrc" class="org.springframework.jndi.JndiObjectFactoryBean" lazy-init="true">
<property name="jndiName" value="java:comp/env/datasource"/>
</bean>
<bean id="daoTxTemplate" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="get*">
PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED
</prop>
</props>
</property>
</bean>
<bean name="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory" ref="hibernateSessionFactory" />
<property name="singleSession" value="true" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="hibernateSessionFactory"/>
<property name="nestedTransactionAllowed" value="true" />
</bean>
答案 0 :(得分:3)
你可以在这里做几件事。如果您使用查询缓存,我建议使用Query.setCacheMode(CacheMode.REFRESH)
。这将强制在查询期间刷新实体。
Hibernate Documentation实际陈述......
这在基础数据可能具有的情况下特别有用 已通过单独的流程更新
您也可以使用SessionFactory.evictQueries()
,但这会删除所有查询缓存,这可能有点过分。
最后,您还可以使用EntityManager.refresh(entity)
重新加载特定实体(如果已知)。
答案 1 :(得分:0)
为什么要让你的Hibernate会话开放这么久?它们不应该打开一天或更长时间。你应该打开会话,读取数据,做你的事情,然后关闭会话。如果您需要在会话之间保留数据,则应使用自己的缓存。
当这些每日更新完成时,您在数据库中保留一些审计跟踪,对吗?因此,每次打开Hibernate会话时,请检查内部缓存是否是最新的。如果自填充缓存以来数据库中的数据已更新,请转储缓存并重新加载。