gpt4 book ai didi

java - 一级缓存不适用于 JPA 和 Hibernate

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

我是一个使用 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
}

如果默认启用一级缓存,这应该执行一次 native 查询。但我在执行此查询时得到以下结果:

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 注释测试将启动事务(绑定(bind)到当前执行的 Spring 逻辑事务与实际物理数据库事务相关的持久性上下文事务)。

当测试方法结束时,事务通常会回滚,因此不会将任何更改传播到任何后续测试,但在当前执行的测试方法中,您会看到自己的更改,即 ACID 中的 Isolation 属性。

关于java - 一级缓存不适用于 JPA 和 Hibernate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23669960/

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