我正在使用hibernate与数据库进行交互。插入,删除,更新操作没有问题,因为它们以提交语句 session.getTransaction.commit()结束。
但是selecting data,listing records
hibernate返回先前显示的数据,并且不显示所有新记录或更新。
所以在问这个问题之前,我曾尝试(两周前)导航到类似的问题,但在应用所有建议时我没有找到答案。
(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;
}
我的hibernate配置文件是:
<?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>
所以我很高兴提出两个问题:
我的方法有什么问题或者我的hibernate配置错了吗?
什么阻止hibernate与数据库同步以获取新的插入记录?
感谢。
答案 0 :(得分:0)
我意识到每个事务都已启动,必须提交以确保hibernate与数据库同步。如果未提交这些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;
}