我有一个与休眠 2 级缓存相关的问题。它适用于 getById 方法,但不适用于其他方法:
这是我的代码:
<块引用>jpa: 数据库平台:org.hibernate.dialect.MySQL5InnoDBDialect 数据库:MYSQL 显示 sql: 真 特性: hibernate.id.new_generator_mappings:真 hibernate.connection.provider_disables_autocommit: true hibernate.cache.use_second_level_cache:真 hibernate.cache.use_query_cache:真 hibernate.cache.region.factory_class: com.mycompany.myapp.config.CustomRegionFactory hibernate.generate_statistics: 真 hibernate.cache.region_prefix:休眠 hibernate.cache.use_structured_entries: 真
@Configuration @EnableCaching public class CacheConfiguration {
@Bean
public javax.cache.configuration.Configuration<Object, Object> jcacheConfiguration(JHipsterProperties jHipsterProperties) {
MutableConfiguration<Object, Object> jcacheConfig = new MutableConfiguration<>();
Config config = new Config();
config.useSingleServer().setAddress(jHipsterProperties.getCache().getRedis().getServer());
jcacheConfig.setStatisticsEnabled(true);
jcacheConfig.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, jHipsterProperties.getCache().getRedis().getExpiration())));
return RedissonConfiguration.fromInstance(Redisson.create(config), jcacheConfig);
}
@Bean
public HibernatePropertiesCustomizer hibernatePropertiesCustomizer(javax.cache.CacheManager cm) {
return hibernateProperties -> hibernateProperties.put(ConfigSettings.CACHE_MANAGER, cm);
}
@Bean
public JCacheManagerCustomizer cacheManagerCustomizer(javax.cache.configuration.Configuration<Object, Object> jcacheConfiguration) {
return cm -> {
createCache(cm, com.mycompany.myapp.repository.UserRepository.USERS_BY_LOGIN_CACHE, jcacheConfiguration);
createCache(cm, com.mycompany.myapp.repository.UserRepository.USERS_BY_EMAIL_CACHE, jcacheConfiguration);
createCache(cm, com.mycompany.myapp.domain.User.class.getName(), jcacheConfiguration);
createCache(cm, com.mycompany.myapp.domain.Authority.class.getName(), jcacheConfiguration);
createCache(cm, com.mycompany.myapp.domain.User.class.getName() + ".authorities", jcacheConfiguration);
createCache(cm, com.mycompany.myapp.domain.Company.class.getName(), jcacheConfiguration);
// jhipster-needle-redis-add-entry
};
}
private void createCache(javax.cache.CacheManager cm, String cacheName, javax.cache.configuration.Configuration<Object, Object> jcacheConfiguration) {
javax.cache.Cache<Object, Object> cache = cm.getCache(cacheName);
if (cache != null) {
cm.destroyCache(cacheName);
}
cm.createCache(cacheName, jcacheConfiguration);
} }
公共类 CustomRegionFactory 扩展 RedissonRegionFactory {
@Override
protected RedissonClient createRedissonClient(Map properties) {
Config config = new Config();
config.useSingleServer().setAddress("redis://127.0.0.1:6379").setRetryInterval(1500)
.setRetryAttempts(3).setConnectTimeout(10000)
.setClientName("client1");
return Redisson.create(config);
}
}
实体:
package com.mycompany.myapp.domain;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import javax.persistence.*;
import java.io.Serializable;
/**
* A Company.
*/
@Entity
@Table(name = "company")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Company implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
// jhipster-needle-entity-add-field - JHipster will add fields here, do not remove
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public Company name(String name) {
this.name = name;
return this;
}
public void setName(String name) {
this.name = name;
}
// jhipster-needle-entity-add-getters-setters - JHipster will add getters and setters here, do not remove
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Company)) {
return false;
}
return id != null && id.equals(((Company) o).id);
}
@Override
public int hashCode() {
return 31;
}
@Override
public String toString() {
return "Company{" +
"id=" + getId() +
", name='" + getName() + "'" +
"}";
}
}
请帮帮我