我有一个名为Article(简化)的后续课程:
@Entity
@DiscriminatorValue("Article")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
class Article {
// .....
@OneToMany(mappedBy = "author.article")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
Set<ArticleAuthorInformation> authors;
}
authors字段创建与User
类的关系。 ArticleAuthorInformation
而不是用户的原因是我需要关于此关系的额外信息(为文章支付的版税)。
这就是它的样子:
@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Cacheable
public class ArticleAuthorInformation implements Serializable {
@Id
ArticleAuthor author;
@Basic(fetch = FetchType.LAZY)
int royalty;
}
ArticleAuthor是一个组合主键,如下所示:
@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;
// .....
}
我在各处添加了大量的@Cache注释,但每当我访问文章中的authors
Set时,以下查询总是由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=?
如果您查看查询,它根本没有任何意义!它基本上只询问royalty
字段(因为它显然知道其他字段的值),我不会在代码中的任何地方访问它。我甚至将royalty
字段标记为懒惰,但Hibernate仍然一直在查询它。
删除royalty
字段会使查询消失,但这不是解决方案,我需要该字段存在...