0

我正在使用休眠与数据库进行交互。插入、删除、更新操作没有问题,因为它们由提交语句session.getTransaction.commit()结束。

但是,虽然selecting data,listing records 休眠返回先前显示的数据并且不显示所有新记录或所做的更新。

所以在问这个问题之前,我尝试(两周前)导航到类似的问题,但在应用所有建议时我没有找到答案。

(A) 启用二级缓存 (b) 增加隔离级别

这对我来说变得很奇怪,因为当我想更新最近插入的记录时,我得到以下信息。

HTTP Status 500 - No row with the given identifier exists: [com.bd.model.TestType#15]

type Exception report
message No row with the given identifier exists: [com.bd.model.TestType#15]

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.bd.model.TestType#15]
    org.hibernate.impl.SessionFactoryImpl$1.handleEntityNotFound(SessionFactoryImpl.java:377)
    org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:79)
    org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:68)
    org.hibernate.Hibernate.initialize(Hibernate.java:306)
    com.bnr.clinic.services.TestTypeServices.getTestTypeById(TestTypeServices.java:79)
    com.bnr.clinic.controller.TestTypeUpdateController.doPost(TestTypeUpdateController.java:85)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.52 logs.

这是我正在使用的一种选择方法!

public TestType getTestTypeById(int idTestType) {
    session = sf.getCurrentSession();
    session.beginTransaction();
    session.clear();
    TestType testTypes = (TestType) session.load(TestType.class, idTestType);
    Hibernate.initialize(testTypes);
   return testTypes;
}

我的休眠配置文件是这样的:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
        <session-factory>
<!-- Database connectivity -->
            <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mis</property>
            <property name="hibernate.connection.username">root</property>
            <property name="hibernate.connection.password">@ict#</property>
            <property name="connection.pool_size">1</property>

            <!-- Enable Hibernate's automatic session context management -->

            <property name="current_session_context_class">thread</property>
            <property name="connection.autocommit">true</property>

<!-- Disabling timeout -->
            <property name="connection.autoReconnect"> true</property>
            <property name="connection.autoReconnectForPools">true</property>
            <property name="c3p0.min_size">5</property>
            <property name="c3p0.max_size">20</property>
            <property name="c3p0.timeout">1800</property>
            <property name="c3p0.max_statements">50</property>
            <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
            <property name="connection.release_mode">auto</property>

            <!-- Disable the second-level cache -->

            <property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
            <property name="hibernate.cache.use_second_level_cache">false</property>
            <property name="hibernate.cache.use_query_cache">true</property>
            <property name="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</property>
    
            <!-- Echo all executed SQL to stdout -->
            <property name="show_sql">true</property>
            <property name="hbm2ddl.auto">update</property>
            <mapping class="com.bd.model.Test" />
            <mapping class="com.bd.model.TestType" />
            
             
        </session-factory>
    </hibernate-configuration>

所以我很高兴提出两个问题:

我的方法有什么问题还是我的休眠配置有问题?

是什么阻止休眠与数据库同步以获取新插入的记录?

谢谢。

4

1 回答 1

0

我意识到每一个事务开始,都必须提交以确保休眠与数据库同步。如果未提交这些 READING(SELECT) 事务并且在将新记录加载到数据库中时您继续获取相同的记录,则 Hibernate 使用缓存。

我的配置很好,唯一的变化是代表服务方法

public TestType getTestTypeById(int idTestType) {
    Session session = sf.openSession(); // sf as a sessionFactory instance
    Transaction tx = null;
    TestType testType = null;
    try {
        tx = session.beginTransaction();
        testType = (TestType) session.get(TestType.class, idTestType);
        tx.commit();
    } catch (HibernateException e) {
        if (tx != null)
            tx.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }

    return testType;
}
于 2014-04-02T07:57:47.040 回答