0
我有OneToOne映射兩個實體定義如下:Hibernate的過濾器不工作
類別
@Entity
@Table(name = "category")
@FilterDef(name = "currentLang", parameters = {
@ParamDef(name = "lang", type = "string")
})
public class Category implements Serializable {
@Id
@NotNull
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;
@Column(name = "dt")
private String dt;
@Column(name = "enable", length = 5)
private String enable;
@OneToOne()
@Filter(name = "currentLang", condition = ":lang = lang")
@JoinColumn(name = "context_id", nullable = false, updatable = false, referencedColumnName = "link")
private Context context;
public int getId() {
return id;//test
}
public void setId(int id) {
this.id = id;
}
public String getDt() {
return dt;
}
public void setDt(String dt) {
this.dt = dt;
}
public String getEnable() {
return enable;
}
public void setEnable(String enable) {
this.enable = enable;
}
public Context getContext() {
return context;
}
public void setContext(Context context) {
this.context = context;
}
public Category() {
}
public Category(String dt, String enable) {
this.dt = dt;
this.enable = enable;
}
}
語境
@Entity
@Table(name = "context")
@Component
@Scope("session")
public class Context implements Serializable {
@Id
@NotNull
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;
@Column(name = "text")
private String text;
@Column(name = "lang")
private String lang;
@Column(name = "link")
private Integer link;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getLang() {
return lang;
}
public void setLang(String lang) {
this.lang = lang;
}
public Context(String text, String lang) {
this.text = text;
this.lang = lang;
}
public Context() {
}
public Integer getLink() {
return link;
}
public void setLink(Integer link) {
this.link = link;
}
}
下面的代碼片段檢索模型類別。
List<com.blog.blog.entity.Category> categories = categoryService.getAll();
com.blog.blog.entity.Category category = categories.get(0);
org.apache.log4j.Logger.getRootLogger().addAppender(new ConsoleAppender(new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN)));
Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());
logger.info(category.getEnable());
logger.info(category.getContext().getText());
但過濾器不工作,結果是錯誤的,並且在日誌中記錄了錯誤的查詢。
Info: Hibernate: select category0_.id as id1_0_, category0_.context_id as context_4_0_, category0_.dt as dt2_0_, category0_.enable as enable3_0_ from category category0_
Info: Hibernate: select context0_.id as id1_1_0_, context0_.lang as lang2_1_0_, context0_.link as link3_1_0_, context0_.text as text4_1_0_ from context context0_ where context0_.link=?
你能該過濾器設置參數? – cy3er
對不起,我忘了加,是的,當然,我啓用了過濾器。 這裏是code org.hibernate.Session session =(Session)entityManager.getDelegate(); \t \t session.enableFilter(「currentLang」)。setParameter(「lang」,「en」); –