1

データベース

motherテーブル

+-----------+------+
| mother_id | name |
+-----------+------+
|         1 | m1   |
|         2 | m2   |
+-----------+------+

childテーブル

+----------+-----------+------+
| child_id | mother_id | name |
+----------+-----------+------+
|        1 |         1 | c1   |
|        2 |         1 | c2   |
|        3 |         2 | c3   |
+----------+-----------+------+

ドメイン

Mother.java

@Entity
@Table(name = "mother")
public class Mother {

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "mother")
    public List<Child> getChilds() {
        return this.childs;
    }

}

Child.java

@Entity
@Table(name = "child")
public class Child {

    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "id_mother", nullable = false)
    public Mother getMother() {
        return this.mother;
    }

}

ダオス

MotherDao.java

public interface MotherDao {

    @Cacheable(cacheName = "dao")
    public List<Mother> findAll();

    @TriggersRemove(cacheName = "dao")
    public void delete(Integer pk);

}

MotherDao.java

public interface ChildDao {

    @Cacheable(cacheName = "dao")
    public List<Child> findAll();

}

会議

applicationContext.xml

<ehcache:annotation-driven cache-manager="ehCacheManager" />    
<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" />

ehcache.xml

<cache name="dao"
    eternal="false"
    maxElementsInMemory="10000"
    overflowToDisk="false"
    timeToIdleSeconds="86400"
    imeToLiveSeconds="86400"
    memoryStoreEvictionPolicy="LFU" />

問題

System.out.println(motherDao.findAll().size());
System.out.println(childDao.findAll().size());
motherDao.delete(1);
System.out.println(motherDao.findAll().size());
System.out.println(childDao.findAll().size());

版画:

2
3
1
3

それ以外の:

2
3
1
1

マザーの削除により 2 つの子がカスケード削除されましたが、以前はchildDao.findAll()その結果がキャッシュされていました。

質問

ドメインが他のドメインと持つ可能性がある1対多の関係のキャッシュをEhCacheにリセットさせる方法は?

4

1 に答える 1

1

これには、2つのキャッシュを使用できる可能性があります。以下に例を示します。

public interface ChildDao {

    @Cacheable(cacheName = "childDao")
    public List<Child> findAll();

}

マザーダオ。

public interface MotherDao {
    @Cacheable(cacheName = "motherDao")
    public List<Mother> findAll();

    @TriggersRemove(cacheName={"motherDao", "childDao"})
    public void delete(Integer pk);
}
于 2013-01-10T18:01:05.810 に答える