以编程方式创建一个hibernate析取查询

我有一大堆java代码,它硬编码一个看起来像这样的hibernate析取查询

session = HibernateUtils.beginTransaction("outpatient"); Criteria criteria = session.createCriteria(AugmentToken.class); session.beginTransaction(); if (type == Constants.ICD9CPT) { criteria.add(Restrictions.disjunction() .add(Restrictions.eq("codeType", "d")) .add(Restrictions.eq("codeType", "p")) .add(Restrictions.eq("codeType", "c"))); } else if (type == Constants.EM) { criteria.add(Restrictions.disjunction() .add(Restrictions.eq("codeType", "eros")) .add(Restrictions.eq("codeType", "ehpi")) .add(Restrictions.eq("codeType", "epe"))); } 

但这不是很优雅的代码。 我想要做的是将一个代码类型数组传递给一个方法,并动态构造dijunction标准。 我看到的每个网站都提供了类似于上面的析取查询示例,但这对我不起作用,因为我不想硬编码标准限制的构造,因为代码类型的数量可能会有所不同。

我该怎么做呢?

谢谢,

埃利奥特

我想我想出来了。 您将析取创建为变量,然后按顺序添加。
特别:

  String [] codeTypes = new String[3]; codeTyes[0]="d"; codeTypes[1]="p"; codetypes[2]="c"; /* note the above would normally be passed into the method containing the code below */ Criteria criteria = session.createCriteria(AugmentToken.class); session.beginTransaction(); Disjunction disjunction = Restrictions.disjunction(); for (int x = 0; x < codeTypes.length; x++ ) { disjucntion.add(Restrictions.eq("codeType",codeTypes[x]); } criteria.add(disjunction); 

我在第214页的Beginning Hibernate中找到了答案。可以从books.google.com访问该书。