最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

caching - Hibernate L2 Cache. Collections - Stack Overflow

programmeradmin11浏览0评论

I'm trying to add L2 Cache to Hibernate. But i faced with following problem:

  • i have entity User, that has collection of Role, assigned to him
  • User and Role are both cacheable with cacheable collections:
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
@Audited
@Entity
@Table(name = "USER")
public class User {
...
    @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
    @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
    @ManyToMany(
            fetch = FetchType.LAZY,
            cascade = {CascadeType.PERSIST, CascadeType.MERGE}
    )
    @JoinTable(name = "USER_ROLE",
            joinColumns = @JoinColumn(name = "USER_ID"),
            inverseJoinColumns = @JoinColumn(name = "ROLE_ID")
    )
    private Set<Role> roles = new HashSet<>();

...
}

@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
@Audited
@Entity
@Table(name = "ROLE")
public class Role {
...
    @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
    @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
    @ManyToMany(
            fetch = FetchType.LAZY,
            cascade = {CascadeType.PERSIST, CascadeType.MERGE},
            mappedBy = "roles"
    )
    private Set<User> users;
...
}

but when i'm trying to remove one of Role and then User with this role (in separate transaction)

roleRepository.deleteById(1111L);

i got error EntityNotFoundException, because cached User still linked to removed Role. What i do wrong?

Best regards.

发布评论

评论列表(0)

  1. 暂无评论