[英]First level cache not working with JPA and Hibernate
我是使用休眠缓存的新手(第一级、第二级和查询缓存)。
我的项目是使用 Spring MVC 和 JPA 配置的。
我正在使用下面的 JUnit 测试用例测试一级缓存。
public class FirstLevelCacheTest
{
private static final Logger logger = LoggerFactory.getLogger(FirstLevelCacheTest.class);
@PersistenceContext
private EntityManager entityManager;
@Test
public void testFirstLevelCacheTest()
{
Long clientId = 1L;
// fetch the client entity from database first time
Client client1 = entityManager.find(Client.class, clientId);
logger.debug("Client Name : {}", client1.getName());
// fetch the client entity again
client1 = entityManager.find(Client.class, clientId);
logger.debug("Client Name : {}", client1.getName());
}
}
我的实体类定义为:
@Entity
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
@Table(name = "client")
public class Client extends BaseModel
{
// instance variable declaration
// getter and setter methods for instance variables
}
如果默认启用一级缓存,这应该执行一次本机查询。 但是我在执行此查询时得到以下结果:
Hibernate: select client0_.id as id0_0_, client0_.created as created0_0_, client0_.createdBy as createdBy0_0_, client0_.updated as updated0_0_, client0_.updatedBy as updatedBy0_0_, client0_.contactNo as contactNo0_0_, client0_.contactPerson as contactP7_0_0_, client0_.contactPersonEmail as contactP8_0_0_, client0_.contactPersonNo as contactP9_0_0_, client0_.email as email0_0_, client0_.name as name0_0_ from client client0_ where client0_.id=?
[main] DEBUG Client Name : Client1
Hibernate: select client0_.id as id0_0_, client0_.created as created0_0_, client0_.createdBy as createdBy0_0_, client0_.updated as updated0_0_, client0_.updatedBy as updatedBy0_0_, client0_.contactNo as contactNo0_0_, client0_.contactPerson as contactP7_0_0_, client0_.contactPersonEmail as contactP8_0_0_, client0_.contactPersonNo as contactP9_0_0_, client0_.email as email0_0_, client0_.name as name0_0_ from client client0_ where client0_.id=?
[main] DEBUG Client Name : Client1
以下是我的持久化相关配置:
@Configuration
@EnableTransactionManagement
public class PersistanceConfig
{
// injection
private String[] PACKAGES_TO_SCAN = new String[] { "com.mypackage1", "com.mypackage2" };
@Bean
public DataSource dataSource()
{
// dataSource setting 'com.mysql.jdbc.Driver' as a jdbc driver
}
private Properties jpaProperties()
{
Properties properties = new Properties();
properties.put(AvailableSettings.DIALECT, hibernateDialect);
properties.put(AvailableSettings.SHOW_SQL, hibernateShowSql);
// 2nd level cache
properties.put("hibernate.cache.use_second_level_cache", true);
properties.put("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.EhCacheRegionFactory");
return properties;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory()
{
// entity manager settings using dataSource and jpaProperties
}
@Bean
public JpaTransactionManager transactionManager()
{
// transaction manager settings using entityManagerFactory
}
}
任何人都可以帮助我解决问题或我做错了什么吗?
提前致谢 !
您需要使用@Transactional
注释测试方法或使用 Spring TransactionTemplate
。
即使检索实体不需要事务,在一个内部对多个调用进行分组也会清楚地指定持久性上下文边界(它打算从哪里开始,应该在哪里结束)。
要复制第一级缓存,您需要在整个测试方法中使用相同的工作单元,并且使用@Transactional
注释测试将启动一个事务(绑定到当前执行的持久性上下文事务的 Spring 逻辑事务,该事务与实际的物理数据库相关)交易)。
当测试方法结束时,事务通常会回滚,因此不会将更改传播到任何后续测试,但在当前执行的测试方法中,您可以看到自己的更改,即 ACID 中的隔离属性。
我已经在一个简单的命令行应用程序中测试了您的场景,但无法重现该问题。 使用 Hibernate 4.0.1 和 Hibernate 4.3 进行测试。 基本上我在事务外两次获取相同的实体,第二个 SQL 查询没有执行。 重要提示:我一个接一个地调用EntityManager.find()
,它们之间没有任何其他操作(比如打开一个事务,触发另一个 SQL 选择的 case)。
我建议您执行以下操作: 1. 确保您还查看数据库服务器的日志(确保 Hibernate 不会记录查询,尽管它不会发送它们)。 2. 将 Hibernate 升级到更新的版本。
另一个注意事项:据我所知,Hibernate 没有要求使用这样的缓存。
我用过的persistence.xml:
<persistence version="2.0" xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="my-PU" transaction-type="RESOURCE_LOCAL">
<!--<provider>org.hibernate.ejb.HibernatePersistence</provider>-->
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.package.Entity</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.hbm2ddl.import_files" valu="sql/import-users.sql"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="false"/>
</properties>
</persistence-unit>
</persistence>
@Transactional 负责处理从 sessionFactory 中获取 session 对象,而 entityManagerFactory 一级缓存中的 entityManger 对象发生在 session 级别或 entityManger 级别,因此 @Tranaction 注释在这里非常重要
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.