简体   繁体   English

休眠查询未返回更新结果

[英]Hibernate query is not returning updated result

In my project, I have two parts UI and Engine. 在我的项目中,我分为UI和引擎两部分。 Both are accessing same Database but different hbm.xml files and hibernate connections. 两者都访问相同的数据库,但是访问不同的hbm.xml文件和休眠连接。 If I try to update some table value from UI, the Engine side query does not return updated value. 如果我尝试从UI更新某些表值,则引擎端查询不会返回更新后的值。

Is it some general problem? 这是一些普遍的问题吗? Below is my code. 下面是我的代码。 I checked it online and found something about Hibernate Cache. 我在线检查了一下,发现了一些有关Hibernate Cache的信息。 I do not know how to remove it. 我不知道如何删除它。 Please help. 请帮忙。

public CgUssdGatewayConf getGwConfig(Long gwId)
{
    logger.info("GW ID : "+gwId);
    Criteria criteria = getSession().createCriteria(CgUssdGatewayConf.class);
    criteria.add(Restrictions.eq("gwId", gwId));
    //Now checking while cache loading time.
    //criteria.add(Restrictions.eq("status", Constants.GW_STATUS_ENABLE));

    List<CgUssdGatewayConf> list = (List<CgUssdGatewayConf>) criteria.list();
    if(list != null && !list.isEmpty())
    {
        CgUssdGatewayConf cgUssdGatewayConf = list.get(0);
        logger.info("GW ID : "+cgUssdGatewayConf.getGwId()+ " :: Name : "+cgUssdGatewayConf.getGwName() + " :: Status : "+cgUssdGatewayConf.getStatus());
        return cgUssdGatewayConf;
    }
    return null;
}

and my hibernate config is- 而我的休眠配置是-

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="hibernateProperties">
        <props>
            <!-- Mysql Config -->
            <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
            <prop key="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/consentgateway</prop>
            <prop key="hibernate.connection.username">cg</prop>
            <prop key="hibernate.connection.password">cg123</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

            <prop key="hibernate.connection.autoReconnect">true</prop>
            <prop key="hibernate.connection.autoReconnectForPools">true</prop>
            <prop key="hibernate.connection.is-connection-validation-required">true</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.cglib.use_reflection_optimizer">false</prop>
            <prop key="hibernate.c3p0.min_size">4</prop>
            <prop key="hibernate.c3p0.max_size">50</prop>
            <prop key="hibernate.c3p0.timeout">0</prop>
            <prop key="hibernate.c3p0.max_statements">0</prop>
            <prop key="hibernate.c3p0.idle_test_period">10800</prop>
            <prop key="hibernate.c3p0.acquire_increment">3</prop>
            <prop key="hibernate.connection.useUnicode">true</prop>
            <prop key="hibernate.connection.characterEncoding">UTF-8</prop>
            <prop key="hibernate.c3p0.maxAdministrativeTaskTime">0</prop>
            <prop key="hibernate.c3p0.acquireRetryAttempts">5</prop>
            <prop key="hibernate.connection.useUnicode">true</prop>
            <prop key="hibernate.connection.characterEncoding">UTF-8</prop>
        </props>
    </property>

    <property name="mappingResources">
        <list>
            <value>CgUssdGatewayConf.hbm.xml</value>
        </list>
    </property>
</bean>

First level caching is default thing in Hibernate and always Associates with the Session object. 一级缓存是Hibernate中的默认设置,并且始终与Session对象关联。 Hibernate uses this cache by default. Hibernate默认使用此缓存。 Here, it processes one transaction after another one. 在这里,它处理另一笔交易。 Mainly it reduces the number of SQL queries it needs to generate within a given transaction. 主要是它减少了在给定事务中需要生成的SQL查询的数量。 That is instead of updating after every modification done in the transaction, it updates the transaction only at the end of the transaction. 那不是在事务中完成每次修改之后更新,而是仅在事务结束时更新事务。

for reflecting cached queries's effect Hibernate Transaction needs to be committed. 为了反映缓存查询的效果,需要提交Hibernate Transaction。

I don't know whether you have committed or not but check for it. 我不知道你是否承诺,但要检查一下。

Transaction trnsction = session.beginTransaction();

update queries

transaction.commit();

The first level cache cannot be disabled at all. 根本不能禁用第一级缓存。 However you can evict the object from the session before loading it. 但是,您可以在加载对象之前将其从会话中 evict As the result the object will be reread from the database. 结果,该对象将从数据库中重新读取。

Session.evict(entity);

Or refresh object. refresh对象。 Also loads the latest object state from the database and rewrites the existing one in the session. 还从数据库加载最新的对象状态,并重写会话中的现有对象状态。

Session.refresh(entity);

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

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