n-m和@version

时间:2017-02-25 05:58:20

标签: java sql hibernate caching

缓存articletag之间的n-m关系,这是我的DDL:

CREATE TABLE TAG_ARTICLE (
  articles_id BIGINT NOT NULL,
  tags_id BIGINT NOT NULL,
  CONSTRAINT "TAG_ARTICLE_pkey" PRIMARY KEY(articles_id, tags_id),
  CONSTRAINT fk6xn1snukol646eslcghksu1s9 FOREIGN KEY (articles_id)
    REFERENCES public.TAG (id)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION
    NOT DEFERRABLE,
  CONSTRAINT fkrecpkpqx4li97ri4xl0e1j9rf FOREIGN KEY (tags_id)
    REFERENCES public.ARTICLE(id)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION
    NOT DEFERRABLE
) 
WITH (oids = false);

这是Article中的m部分:

/**
 * The m-part of the n-m relation. The non-owning-side.
 */
@Deprecated
@ManyToMany(mappedBy="articles")
public Set<Tag> getTags() {
    return tags;
}

这是我Tag中的N面:

/**
 * The n-part of the n-m relation. The owning-side.
 */
@ManyToMany
@JoinTable(name=Article, joinColumns = {
    @JoinColumn(name=ARTICLES_FK, referencedColumnName = ID_FIELD_NAME, nullable = false)
}, inverseJoinColumns = {
    @JoinColumn(name=Article.TAGS_FK, referencedColumnName = Article.ID_FIELD_NAME, nullable = false)
})
public Set<Article> getArticles() {
    return articles;
}

效果很好。我在两个表中都有很多条目,然后我引入@Version字段来检查是否必须清除我的内部缓存

SELECT max(version) FROM TAG;
SELECT max(version) FROM ARTICLE;

不幸的是TAG_ARTICLE没有version - 列,但如果某个成员不再是某个团队成员,我需要清除缓存。

什么是好的解决方案?

0 个答案:

没有答案