Hibernate Ehcache不使用缓存数据

时间:2017-03-05 13:13:44

标签: hibernate jpa ehcache

我正在尝试让ehcache在非Spring环境中工作(使用Hibernate Entity Manager的Swing Client)。

缓存似乎有效,但由于任何原因,缓存会将所有查询发送到数据库,而不是使用缓存的值。

但我不知道为什么。

任何帮助将不胜感激。

这是我的环境:

10 9 8 7 6 5 4 3 2 1 
9 8 7 6 5 4 3 2 1 
8 7 6 5 4 3 2 1 
7 6 5 4 3 2 1 
6 5 4 3 2 1 
5 4 3 2 1 

ehcache.xml中

def print_triangle(max_val, min_val):
    for row in range(max_val, min_val - 1, -1):
        for num in range(row, 0, -1):
            print(str(num) + " ", end="")
        print()


print_triangle(10, 5)

的persistence.xml

 <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.38</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.hibernate.common</groupId>
      <artifactId>hibernate-commons-annotations</artifactId>
      <version>4.0.4.Final</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>4.3.11.Final</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-annotations</artifactId>
      <version>3.5.6-Final</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>4.3.11.Final</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-ehcache</artifactId>
      <version>4.3.11.Final</version>
      <scope>compile</scope>
    </dependency>

Customer.java

 <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
            monitoring="autodetect" dynamicConfig="true">

         <defaultCache
                maxElementsInMemory="10000"
                eternal="false"
                timeToIdleSeconds="120"
                timeToLiveSeconds="120"
                overflowToDisk="false"
                diskSpoolBufferSizeMB="30"
                diskPersistent="false"
                diskExpiryThreadIntervalSeconds="120"
                memoryStoreEvictionPolicy="LRU"
                />
    </ehcache>

测试

 <?xml version="1.0" encoding="UTF-8" ?>
    <persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
      version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">

      <persistence-unit name="jaccount" transaction-type="RESOURCE_LOCAL">
          <class>ch.bemar.account.persistence.model.Account</class>
          <class>ch.bemar.account.persistence.model.AccountId</class>
          <class>ch.bemar.account.persistence.model.Booking</class>
          <class>ch.bemar.account.persistence.model.BookingId</class>
          <class>ch.bemar.account.persistence.model.Category</class>
          <class>ch.bemar.account.persistence.model.CategoryId</class>
          <class>ch.bemar.account.persistence.model.Property</class>
          <class>ch.bemar.account.persistence.model.Type</class>
          <class>ch.bemar.account.persistence.model.Year</class>
          <class>ch.bemar.account.persistence.model.YearId</class>
          <class>ch.bemar.account.persistence.model.I18n</class>
          <class>ch.bemar.account.persistence.model.I18nGroup</class>
          <class>ch.bemar.account.persistence.model.I18nItem</class>
          <class>ch.bemar.account.persistence.model.Language</class>
          <class>ch.bemar.account.persistence.model.User</class>
          <class>ch.bemar.account.persistence.model.UserId</class>
          <class>ch.bemar.account.persistence.model.Payment</class>
          <class>ch.bemar.account.persistence.model.UserType</class>
          <class>ch.bemar.account.persistence.model.Customer</class>
          <class>ch.bemar.account.persistence.model.PaymentId</class>
          <class>ch.bemar.account.persistence.model.PropertyId</class>
          <class>ch.bemar.account.persistence.model.UserTypeId</class>

          <properties>
              <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
              <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jaccount_test" />
              <property name="javax.persistence.jdbc.user" value="jaccount_test" />
              <property name="javax.persistence.jdbc.password" value="testpw" />

              <property name="hibernate.show_sql" value="true" />

              <property name="hibernate.cache.provider_configuration_file_resource_path"
                  value="ehcache.xml" />
              <property name="hibernate.cache.use_second_level_cache"
                  value="true" />
              <property name="hibernate.cache.use_query_cache" value="false" />
              <property name="hibernate.cache.region.factory_class"
                  value="org.hibernate.cache.ehcache.EhCacheRegionFactory" />
              <property name="hibernate.cache.provider_class" value="org.hibernate.cache.ehcache.EhCacheProvider" />
              <property name="hibernate.generate_statistics" value="true"/>
          </properties>

      </persistence-unit>
    </persistence>

测试输出

@Entity
@Cache(region = "customer", usage = CacheConcurrencyStrategy.READ_ONLY)
@Table(name = "customer")
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "custermer_id", unique = true, nullable = false)
    private Long customerId;

    @Column(name = "customer_name", nullable = false)
    private String customerName;

    @Column(name = "customer_descr")
    private String customerDescripton;

    @Column(name = "customer_selected", nullable = false)
    private Boolean selected;

    public Long getCustomerId() {
        return this.customerId;
    }

    public void setCustomerId(Long customerId) {
        this.customerId = customerId;
    }

    public String getCustomerName() {
        return this.customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public String getCustomerDescripton() {
        return this.customerDescripton;
    }

    public void setCustomerDescripton(String customerDescripton) {
        this.customerDescripton = customerDescripton;
    }

    public Boolean getSelected() {
        return this.selected;
    }

    public void setSelected(Boolean selected) {
        this.selected = selected;
    }

感谢您的帮助

本杰明

1 个答案:

答案 0 :(得分:0)

确定。那太尴尬了。我按

打开查询缓存后
<property name="hibernate.cache.use_query_cache" value="true" />

它奏效了。

谢谢你们。