Hibernate @OneToMany(mappedBy="...") relation not being cached
I have开发者_运维知识库 a following class called Article (simplified):
@Entity
@DiscriminatorValue("Article")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
class Article {
// .....
@OneToMany(mappedBy = "author.article")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
Set<ArticleAuthorInformation> authors;
}
The authors field creates a relation to the User
class. The reason why there is ArticleAuthorInformation
instead of User is that I need extra information for this relation (the royalty paid for the article).
This is what it looks like:
@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Cacheable
public class ArticleAuthorInformation implements Serializable {
@Id
ArticleAuthor author;
@Basic(fetch = FetchType.LAZY)
int royalty;
}
ArticleAuthor is a combined primary key as follows:
@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;
// .....
}
I've added tonnes of @Cache annotations everywhere, but whenever I access the authors
Set in Article, the following query is always made by Hibernate:
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=?
If you take a look at the query, it doesn't make any sense at all! It is basically asking only for the royalty
field (as it apparently knows the value of other fields), which I'm not accessing anywhere in the code. I've even marked the royalty
field as lazy, but Hibernate still keeps querying for it.
Removing the royalty
field makes the query go away, but that's not a solution, I will need the field to exist...
精彩评论