如何在Hibernate中为延迟加​​载的集合使用二级缓存?

时间:2010-02-18 10:57:23

标签: java hibernate orm lazy-loading second-level-cache

假设我有两个实体EmployeeSkill。每个员工都有一套技能。现在,当我通过Employee实例懒洋洋地加载技能时,缓存不会用于Employee的不同实例中的技能。

让我们考虑以下数据集。

Employee - 1 : Java, PHP
Employee - 2 : Java, PHP

当我在Employee-1之后加载Employee-2时,我不希望hibernate访问数据库以获取技能,而是使用缓存中已有的Skill实例。这可能吗?如果是这样的话?

休眠配置

<session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.password">pass</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost/cache</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>

    <property name="hibernate.cache.use_second_level_cache">true</property>
    <property name="hibernate.cache.use_query_cache">true</property>
    <property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <property name="hibernate.show_sql">true</property>

    <mapping class="org.cache.models.Employee" />
    <mapping class="org.cache.models.Skill" />
</session-factory>

已删除导入,获取和设置的实体

@Entity
@Table(name = "employee")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Employee {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    private String name;

    public Employee() {
    }

    @ManyToMany
    @JoinTable(name = "employee_skills", joinColumns = @JoinColumn(name = "employee_id"), inverseJoinColumns = @JoinColumn(name = "skill_id"))
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    private List<Skill> skills;
}

@Entity
@Table(name = "skill")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Skill {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    private String name;
}

用于加载第二名员工的SQL及其技能

Hibernate: select employee0_.id as id0_0_, employee0_.name as name0_0_ from employee employee0_ where employee0_.id=?
Hibernate: select skills0_.employee_id as employee1_1_, skills0_.skill_id as skill2_1_, skill1_.id as id1_0_, skill1_.name as name1_0_ from employee_skills skills0_ left outer join skill skill1_ on skills0_.skill_id=skill1_.id where skills0_.employee_id=?

因为我特别希望避免第二个查询,因为第一个查询无论如何都是不可避免的。

1 个答案:

答案 0 :(得分:4)

您需要缓存Employee--<>Skills关联。摘自以下Speed Up Your Hibernate Applications with Second-Level Caching

<hibernate-mapping package="com.wakaleo.articles.caching.businessobjects">
    <class name="Employee" table="EMPLOYEE" dynamic-update="true">
        <meta attribute="implement-equals">true</meta>    

      <id name="id" type="long" unsaved-value="null" >
            <column name="emp_id" not-null="true"/>
            <generator class="increment"/>
      </id>

    <property column="emp_surname" name="surname" type="string"/>
    <property column="emp_firstname" name="firstname" type="string"/>

    <many-to-one name="country"
            column="cn_id"
                class="com.wakaleo.articles.caching.businessobjects.Country"  
            not-null="true" />

    <!-- Lazy-loading is deactivated to demonstrate caching behavior -->    
    <set name="languages" table="EMPLOYEE_SPEAKS_LANGUAGE" lazy="false">
        <cache usage="read-write"/>
        <key column="emp_id"/>
            <many-to-many column="lan_id" class="Language"/>
    </set>                              
    </class>
</hibernate-mapping>

请注意语言中的<cache>元素。