Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate @OneToMany(mappedBy="...") relation not being cached
    text
    copied!<p>I have a following class called Article (simplified):</p> <pre><code>@Entity @DiscriminatorValue("Article") @Cacheable @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) class Article { // ..... @OneToMany(mappedBy = "author.article") @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) Set&lt;ArticleAuthorInformation&gt; authors; } </code></pre> <p>The authors field creates a relation to the <code>User</code> class. The reason why there is <code>ArticleAuthorInformation</code> instead of User is that I need extra information for this relation (the royalty paid for the article).</p> <p>This is what it looks like:</p> <pre><code>@Entity @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) @Cacheable public class ArticleAuthorInformation implements Serializable { @Id ArticleAuthor author; @Basic(fetch = FetchType.LAZY) int royalty; } </code></pre> <p>ArticleAuthor is a combined primary key as follows:</p> <pre><code>@Embeddable @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) @Cacheable public class ArticleAuthor implements Serializable { @ManyToOne @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) private User user; @ManyToOne @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) private Article article; // ..... } </code></pre> <p>I've added tonnes of @Cache annotations everywhere, but whenever I access the <code>authors</code> Set in Article, the following query is always made by Hibernate:</p> <pre>Hibernate: select articleaut0_.article_id as article2_2_0_, articleaut0_.user_login as user3_2_0_, articleaut0_.royalty as royalty2_0_ from ArticleAuthorInformation articleaut0_ where articleaut0_.article_id=? and articleaut0_.user_login=? </pre> <p>If you take a look at the query, it doesn't make any sense at all! It is basically asking only for the <code>royalty</code> field (as it apparently knows the value of other fields), which I'm not accessing anywhere in the code. I've even marked the <code>royalty</code> field as lazy, but Hibernate still keeps querying for it.</p> <p>Removing the <code>royalty</code> field makes the query go away, but that's not a solution, I will need the field to exist...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload