【发布时间】:2017-10-27 11:32:36
【问题描述】:
我有实体 FormTable 与另一个实体 FormInLangagueTable 有一对多关系,我想过滤掉语言实体(基于动态参数),并且返回具有匹配过滤器参数的语言对象列表的最终对象。
我设法通过 @Filter 和 @FilterDef 实现了这一点,它按预期工作,除了缓存没有发生并且数据库得到了命中每个查询 .
我找到了following JIRA issue,看来我不是第一个遇到这个问题的...
我完全迷路了,如何根据动态参数过滤 FormInLangagueTable 的列表,同时保持 2 级缓存?
谢谢!
表格:
@Entity
@Table(name = "FORM")
@FilterDef(name = "userLanguageFilter", parameters = {
// Set by the dao, to filter out irrelevant captions
@ParamDef(name = "languageIdParam", type = "short")
})
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
public class FormTable {
private Integer formId;
...
/**
* All the available language translations for the given form, won't be presented in the json response.
*/
private List<FormInLanguageFactoryTable> formInLanguages;
/**
* Manually fetched from {@link #formInLanguages}, the language used
* <b>specific to the user</b>, will be returned by the json.
*/
private String specificLanguageCaption;
@Id
@Column(name = "_FormId")
public Integer getFormId() {
return formId;
}
public void setFormId(Integer formId) {
this.formId = formId;
}
/**
* Get all the form title in the corresponding languages, then filter it only to the current user language.
* It <b>will not be presented in the final json,
* but used in {@link #getSpecificLanguageCaption} to get the string, or null if none found.</b>
*/
@OneToMany
@JoinColumn(name = "_FormId", referencedColumnName = "_FormId")
@Filter(name = "userLanguageFilter", condition = "_LanguageId = :languageIdParam")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@JsonIgnore
public List<FormInLanguageTable> getFormInLanguages() {
return formInLanguages;
}
public void setFormInLanguages(List<FormInLanguageTable> languageCaption) {
this.formInLanguages = languageCaption;
}
/**
* Will convert {@link #getFormInLanguages} into a string (the form caption, or null if none found)
* @return
*/
@Transient
@JsonProperty("languageCaption")
public String getSpecificLanguageCaption() {
return this.getFormInLanguages().isEmpty() ? null : this.getFormInLanguages().get(0).getCaption();
}
}
FormInLanguage:
/**
* The available languages for the given form
*/
@Entity
@Table(name = "FormInLanguage")
@Cache(usage= CacheConcurrencyStrategy.READ_ONLY)
public class FormInLanguageTable {
private PK id;
private String caption;
@EmbeddedId
@JsonIgnore
public PK getId() {
return id;
}
public void setId(PK id) {
this.id = id;
}
@Column(name = "Caption")
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
@Embeddable
public static class PK implements Serializable {
private Integer formId;
private Short languageId;
@Column(name = "_FormId")
@JsonIgnore
public Integer getFormId() {
return formId;
}
public void setFormId(Integer formId) {
this.formId = formId;
}
@Column(name = "_LanguageId")
public Short getLanguageId() {
return languageId;
}
public void setLanguageId(Short formFieldId) {
this.languageId = formFieldId;
}
}
}
【问题讨论】:
标签: hibernate caching spring-boot filter one-to-many