2017-04-14 198 views
0

我目前在我的springboot應用程序(版本1.5.2.RELEASE)中使用hibernate二級緩存hazelcast 3.7.5。 每當我使用由Spring Data Jpa實現的findAll()方法時,hibernate會從數據庫中檢索數據,但是當我使用findOne(id)方法時,hibernate會從tha chache中獲取數據。有人能解釋這種奇怪的行爲嗎? 這裏是我的Hibernate配置springboot + hazelcast + hibernate的findAll()從數據庫緩存中取回數據不緩存

spring: 
datasource: 
    url: jdbc:h2:file:./target/h2db/db/parametrage;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS PARAM 
    username: sa 
    password: 
jpa: 
    open-in-view: false 
    show-sql: true 
    hibernate: 
     ddl-auto: none 
     naming: 
      implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl 
      physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl 
    properties: 
     hibernate.default_schema: PARAM 
     hibernate.id.new_generator_mappings: true 
     hibernate.cache.use_second_level_cache: true 
     hibernate.cache.use_query_cache: false 
     hibernate.generate_statistics: true 
     hibernate.cache.region.factory_class: com.hazelcast.hibernate.HazelcastCacheRegionFactory 
     hibernate.cache.hazelcast.instance_name: hazelcast 
     hibernate.cache.use_minimal_puts: true 
     hibernate.cache.hazelcast.use_lite_member: true 

這裏是我的hazelcast配置

@Configuration 
@EnableCaching 

public class CacheConfiguration { 

private final Logger log = LoggerFactory.getLogger(CacheConfiguration.class); 

private final Environment env; 

public CacheConfiguration(Environment env) { 
    this.env = env; 
} 

@Bean 
public CacheManager cacheManager(HazelcastInstance hazelcastInstance) { 
    log.debug("Starting HazelcastCacheManager"); 
    CacheManager cacheManager = new HazelcastCacheManager(hazelcastInstance); 
    return cacheManager; 
} 

@PreDestroy 
public void destroy() { 
    log.info("Closing Cache Manager"); 
    Hazelcast.shutdownAll(); 
} 

@Bean 
public HazelcastInstance hazelcastInstance() { 
    log.debug("Configuring Hazelcast"); 
    Config config = new Config(); 
    config.setInstanceName("hazelcast"); 
    config.getNetworkConfig().setPort(5701); 
    config.getNetworkConfig().setPortAutoIncrement(true); 

    config.getNetworkConfig().getJoin().getAwsConfig().setEnabled(false); 
    config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false); 
    config.getNetworkConfig().getJoin().getTcpIpConfig().addMember("localhost").setEnabled(true); 

    config.getMapConfigs().put("default", initializeDefaultMapConfig()); 
    config.getManagementCenterConfig(). 
    setUrl("http://localhost:8080/mancenter") 
    .setEnabled(true); 

    return Hazelcast.newHazelcastInstance(config); 
} 

private MapConfig initializeDefaultMapConfig() { 
    MapConfig mapConfig = new MapConfig(); 
mapConfig.setBackupCount(0); 

    mapConfig.setEvictionPolicy(EvictionPolicy.LRU); 


    mapConfig.setMaxSizeConfig(new MaxSizeConfig(0, MaxSizeConfig.MaxSizePolicy.USED_HEAP_SIZE)); 

    return mapConfig; 
} 
} 

,並在最後這裏是一個實體

@Entity 
@Table(name = "Banque") 
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) 

public class Banque implements Serializable { 

private static final long serialVersionUID = 1L; 

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
@Basic(optional = false) 

@Column(name = "Code") 
private Integer code; 
@Basic(optional = false) 
@NotNull 
@Size(min = 1, max = 150) 
@Column(name = "Designation") 
private String designation; 
@OneToMany(mappedBy = "codeBanque") 
@JsonBackReference 
private Collection<Societe> societeCollection; 

public Banque() { 
} 

public Banque(Integer code) { 
    this.code = code; 
} 

public Banque(Integer code, String designation) { 
    this.code = code; 
    this.designation = designation; 
} 

public Integer getCode() { 
    return code; 
} 

public void setCode(Integer code) { 
    this.code = code; 
} 

public String getDesignation() { 
    return designation; 
} 

public void setDesignation(String designation) { 
    this.designation = designation; 
} 

public Collection<Societe> getSocieteCollection() { 
    return societeCollection; 
} 

public void setSocieteCollection(Collection<Societe> societeCollection) { 
    this.societeCollection = societeCollection; 
} 

@Override 
public int hashCode() { 
    int hash = 0; 
    hash += (code != null ? code.hashCode() : 0); 
    return hash; 
} 

@Override 
public boolean equals(Object object) { 
    // TODO: Warning - this method won't work in the case the id fields are not set 
    if (!(object instanceof Banque)) { 
     return false; 
    } 
    Banque other = (Banque) object; 
    if ((this.code == null && other.code != null) || (this.code != null && !this.code.equals(other.code))) { 
     return false; 
    } 
    return true; 
} 

@Override 
public String toString() { 
    return "com.csys.parametrage.domain.Banque[ code=" + code + " ]"; 
} 

}

+0

的代碼你'use_query_cache'假 –

+0

我「V切換,但仍是同樣的問題 –

+0

你可以發佈更多的細節,那麼請? 'pom.xml'加上任何其他配置文件,如'hibernate.cfg.xml','persistence.xml'。這或全副本樣本將幫助 –

回答

0

解決的辦法是,像尼爾·史蒂文森在他的評論中說,使用查詢緩存。 要做到這一點,我將hibernate.cache.use_query_cache轉換爲true,但是我也指定要休眠哪些查詢必須使用@QueryHints進行緩存。 這裏是我的倉庫

Repository("BanqueRepository") 
public interface BanqueRepository extends JpaRepository<Banque, Integer> { 

public Banque findByCode(Integer code); 

@Override 
@QueryHints({ 
    @QueryHint(name = "org.hibernate.cacheable", value = "true")}) 
public List<Banque> findAll(); 
} 
0

我得到的一個例子這工作。您需要緩存集合。 我改變

@OneToMany(mappedBy = "codeBanque") 
private Collection<Societe> societeCollection; 

@Cache (usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) 
@OneToMany(fetch = FetchType.EAGER, mappedBy = "codeBanque") 
private Collection<Societe> societeCollection; 
+0

我的目的是要獲得「銀行」的名單 –

+0

我找到一個解決方案,謝謝你的第一條評論:)謝謝你man:D –