以编程方式创建一个hibernate析取查询 [英] Creating a hibernate disjunction query programatically
问题描述
我有一块java代码,它编写了一个hibernate分解查询,看起来像这样。
session = HibernateUtils.beginTransaction ( 门诊病人);
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条件。我所看到的每个网站都提供了类似于上述的分离查询的示例,但这对我来说不起作用,因为我不想为代码的限制构建硬编码,因为代码类型的数量可能会有所不同。
我该怎么做?
谢谢,
Elliott
我想我明白了这一点。你创建一个变量的析取,然后顺序地添加它。
具体来说:
String [] codeTypes = new String [3];
codeTyes [0] =d;
codeTypes [1] =p;
codetypes [2] =c;
/ *注意上面通常会传入包含以下代码的方法* /
Criteria criteria = session.createCriteria(AugmentToken.class);
session.beginTransaction();
Disjunction disjunction = Restrictions.disjunction();
for(int x = 0; x disjucntion.add(Restrictions.eq(codeType,codeTypes [x]);
}
criteria.add(disjunction);
我在第214页的Beginning Hibernate中找到了答案。 book可从books.google.com访问。
I have a chunk of java code which hard codes a hibernate disjunction query that looks like this
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")));
}
But this is not very elegant code. What I would like to do is pass an array of codetypes to a method, and dynamically construct the dijunction criteria. Every website I look at provides examples of disjunctive queries that look like the above, but this will not work for me because I don't want to hard code the construction of the restriction for the criteria since the number of code types can vary.
How do I do this?
Thank you,
Elliott
I think I figured this out. You create the disjunction as a variable, then sequentially add to it.
Specifically:
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);
I found the answer in Beginning Hibernate on page 214. The book is accessible from books.google.com.
这篇关于以编程方式创建一个hibernate析取查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!