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.