I am fairly new to ehcache and as far as I know on method level the cacheing is working on the id of object to be cached. However I have a different situation.
I have a User class and a role class, the relation between them is many-to-many and stored in another table by ids.
public class User{
@Id
@Column(name="ID")
@GeneratedValue(generator="idGenerator",strategy = GenerationType.SEQUENCE)
private Long id;
...
@ManyToMany(targetEntity = Role.class, fetch = FetchType.EAGER)
@JoinTable(name = "ROLE", joinColumns = { @JoinColumn(name = "USER_ID") }, inverseJoinColumns = { @JoinColumn(name = "ROLE_ID") })
protected Set<Role> roles;
...
}
public class Role{
@Id
@Column(name="ID")
@GeneratedValue(generator="idGenerator",strategy = GenerationType.SEQUENCE)
private Long id;
...
@ManyToMany(targetEntity = User.class)
@JoinTable(name = "USER", joinColumns = { @JoinColumn(name = "ROLE_ID") }, inverseJoinColumns = { @JoinColumn(name = "USER_ID") })
protected Set<Role> roles;
...
}
Just to mention, there no such a UserRole domain class.
And the service method is somethig like;
public RoleService{
@Cacheable
public Set<Role> getUserRoles(Long userId)
{
...
}
}
The thing is that on the service level I want the roles to be cached per userId basis. How can I cache roles using id of the user?