title: Caching an entity with spring data jpa
body: <p>I have defined roles in my database <code>role_template</code>.</p>
  
  <pre><code>@Entity
  @Table(name = "role_template")
  @Cacheable
  public class Role {
      @Id
      private int id;
      private String name;
  
      @Transient
      private final int identity = new Random().nextInt(1000000) + 1;
  }
  </code></pre>
  
  <p>I have one role at this moment with <code>id=1 and name="admin"</code><br>
  My entity <code>User</code> has a list of roles defined as follow</p>
  
  <pre><code>@Entity
  @Table(name = "app_user")
  public class User {
      [...]
  
      @ManyToMany(fetch = FetchType.EAGER)
      @JoinTable(name = "role_assign",
          joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
          inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"))
      private Set&lt;Role&gt; roles;
  }
  </code></pre>
  
  <p><code>Roles</code> are joined to users with my association table</p>
  
  <pre><code>[Table `role_assign`]
     int user_id
     int role_id
  </code></pre>
  
  <p>My problem is predictable, <code>@Cacheable</code> does not work.
  I tried with 2 users, they have the same <code>Role</code> template, but not the same <code>instance</code>. The transient variable <code>identity</code> isn't equals for the role of the two users.. My app configuration is good, I think I forgot something to make it working for <code>@JoinTable</code></p>