Hibernate 如何使用filter

@Entity
@Table(name = "T_SEN_DISABILITY_TYPE")
在使用filter的时候还需要定义filter参数。 name 为filter名称,parameters为filter参数,@ParamDef为参数定义,其中那么为appOrCase(参数名称) type为该参数类型
@FilterDef(name = "appOrCaseFilter", parameters = @ParamDef(name = "appOrCase", type = "string"))
public class DisabilityType extends IdEntity {
private List<SpecNeedQuestion> specNeedQuestions;

@OneToMany(fetch = FetchType.LAZY)
@Filters( { @Filter(name = "appOrCaseFilter", condition = ":appOrCase=app_or_case") })
@JoinColumn(name = "DIS_TYPE_ID")
@OrderBy("qstIndex")
public List<SpecNeedQuestion> getSpecNeedQuestions() {
return specNeedQuestions;
}

public void setSpecNeedQuestions(List<SpecNeedQuestion> specNeedQuestions) {
this.specNeedQuestions = specNeedQuestions;
}
@Filters定义该对象关联specNeedQuestion对象是对应的所有的filters。
@Filter 指定单个的filter 属性:name为filter的名字。Condition为filter的过滤条件,其中:app_or_case为SpecNeedQuestion对象对应的表的字段,:appOrCase为参数名称。

public List<DisabilityType> getDisables(String appOrCase) {
String hql = "from DisabilityType t order by t.type";
Session session = this.getSession();
Filter filter = session.enableFilter("appOrCaseFilter");
filter.setParameter("appOrCase", appOrCase);
return find(hql);
}

你可能感兴趣的:(Hibernate)