开发者

Hibernate @OneToMany(mappedBy="...") relation not being cached

开发者 https://www.devze.com 2023-03-25 13:16 出处:网络
I have开发者_运维知识库 a following class called Article (simplified): @Entity @DiscriminatorValue(\"Article\")

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...

0

精彩评论

暂无评论...
验证码 换一张
取 消