gpt4 book ai didi

Hibernate - 使实体与批处理数据库更新保持同步

转载 作者:行者123 更新时间:2023-12-04 06:20:20 24 4
gpt4 key购买 nike

嗨,我希望就以下问题获得一些意见。我是 Hibernate 的新手,并试图为此拼凑拼图。

问题:我的数据库中有每天更新一次的数据。我想让我的实体与此同步并刷新它们。我实现了一个 Spring/Quartz 计时器来调用我的 hibernate 实现类来刷新实体。我试图确保每次调用此方法时都会清除所有当前实体,以便可以刷新它们。什么是最好的方法?

使用与 Spring 集成的 Hibernate 3.2。

建议的解决方案:

  • 这是我将使用实体管理器来管理它们的地方吗?

  • 我尝试在 Session.flush 命令和 SesionFactory clear 上使用,但没有用。

    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>

    最佳答案

    你可以在这里做几件事。我建议使用 Query.setCacheMode(CacheMode.REFRESH)如果您使用查询缓存。这将在查询期间强制刷新实体。

    Hibernate Documentation实际表示...

    This is particularly useful in cases where underlying data may have been updated via a separate process



    您也可以使用 SessionFactory.evictQueries()但这将删除所有查询缓存,这可能有点矫枉过正。

    最后,您还可以使用 EntityManager.refresh(entity)如果已知,则重新加载特定实体。

    关于Hibernate - 使实体与批处理数据库更新保持同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6665587/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com