---
title: "Hibernate 如何使用filter"
author: "bd2007"
date: "2011-08-05T13:46:15Z"
tags: []
categories: ["企业架构"]
slug: "hibernate-filter"
draft: false
lastmod: "2011-08-05T13:46:15Z"
id: 1140940
url: "https://bd2007.iteye.com/blog/1140940"
---

@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);
	}