根实体及其集合的不同缓存并发策略(Hibernate 与 EHCache)?

发布于 2024-08-30 10:53:10 字数 557 浏览 7 评论 0原文

给出的示例来自 Hibernate docs 并修改它,以便根级实体(客户)是只读的,而其集合之一(票证)是读写的:

@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
public class Customer { 
    ... 
    @OneToMany(...)
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    public SortedSet<Ticket> getTickets() {
        return tickets;
    }
    ...
}

从缓存访问客户时,票证集合会刷新吗?

Given example from Hibernate docs and modifying it so that root level entity (Customer) is read-only while one of its collections (tickets) is read-write:

@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
public class Customer { 
    ... 
    @OneToMany(...)
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    public SortedSet<Ticket> getTickets() {
        return tickets;
    }
    ...
}

Would collection of tickets get refreshed when accessing customer from cache?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟凡古楼 2024-09-06 10:53:10

如果您在其他地方修改给定客户的一张票据,是的。你为什么不测试一下这个?我们假设 Cutomer#1 在其 tickets 集合中具有 Ticket#1Ticket#2。我运行了这段代码:

// loading in first session
Session session1 = HibernateUtil.getSession();
Transaction tx1 = session1.beginTransaction();
Customer c1 = (Customer) session1.load(Customer.class, 1L); // loads from db and puts in cache
for (Ticket ticket : c1.getTickets()) { // caches q2742145.Customer.tickets#1
    System.out.println(ticket);
}
Ticket ticket = (Ticket) session1.load(Ticket.class, 1L); // doesn't hit the db
ticket.setName("foo"); // do some change on Ticket#1
session1.save(ticket);
tx1.commit(); // Ticket#1 gets updated in the db and the cached association invalidated

// loading in second session
Session session2 = HibernateUtil.getSession();
Transaction tx2 = session2.beginTransaction();
Customer c2 = (Customer) session2.load(Customer.class, 1L); // hits the cache
Set<Ticket> tickets = c2.getTickets();
for (Ticket ticket2 : tickets) { // reloads tickets from db
    System.out.println(ticket2); 
}
tx2.commit();

这表明票证集合被“刷新”。

If you modify one of the tickets of a given Customer somewhere else, yes. Why don't you test this? Let's assume Cutomer#1 has Ticket#1 and Ticket#2 in its tickets collection. I ran this code:

// loading in first session
Session session1 = HibernateUtil.getSession();
Transaction tx1 = session1.beginTransaction();
Customer c1 = (Customer) session1.load(Customer.class, 1L); // loads from db and puts in cache
for (Ticket ticket : c1.getTickets()) { // caches q2742145.Customer.tickets#1
    System.out.println(ticket);
}
Ticket ticket = (Ticket) session1.load(Ticket.class, 1L); // doesn't hit the db
ticket.setName("foo"); // do some change on Ticket#1
session1.save(ticket);
tx1.commit(); // Ticket#1 gets updated in the db and the cached association invalidated

// loading in second session
Session session2 = HibernateUtil.getSession();
Transaction tx2 = session2.beginTransaction();
Customer c2 = (Customer) session2.load(Customer.class, 1L); // hits the cache
Set<Ticket> tickets = c2.getTickets();
for (Ticket ticket2 : tickets) { // reloads tickets from db
    System.out.println(ticket2); 
}
tx2.commit();

Which shows the collection of tickets gets "refreshed".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文