1

我有一个 Java 类,其中有一些用@SearchCriteria(criteria = "class1.class2.field"). criteria注释内的参数意味着对于类,该字段应设置为休眠条件,这意味着,如果字段标记例如:

@SearchCriteria(criteria = "class1.class2.field")
private String value;

我想动态创建休眠条件,看起来像

DetachedCriteria hibernateCriteria = forClass(Class.class);
hibernateCriteria.createCriteria("class1").createCriteria("class2").add(eq("field", value));

问题是我不能设置另一个已经添加的标准,这意味着我应该检查注释criteria选项。

      switch (annotationCriteria.length - 1) {
case 0:             
hibernateCriteria.add(isNull(annotationCriteria[0]));               
case 1:
hibernateCriteria.createCriteria(annotationCriteria[0]).add(                Restrictions.isNull(annotationCriteria[annotationCriteria.length - 1]));
case 2:   hibernateCriteria.createCriteria(annotationCriteria[0]).createCriteria(annotationCriteria[1]).add(                Restrictions.isNull(annotationCriteria[annotationCriteria.length - 1]));
    }

我想删除这个“开关”。可以通过(我现在不知道)获得已经添加的条件,例如通过“名称”并为其添加新的子条件。

4

2 回答 2

1

不,这是不可能的。Detached Criteria API不允许 - 无论好坏 - 允许发现,因此您将无法向它询问“现有”标准。

但是,您可以做的是通过关联路径维护您自己的嵌套条件图。在伪代码中:

Map<String, DetachedCriteria> criteriaMap = ...;

for ( ) { // loop over annotation criteria's "elements"
    DetachedCriteria existing = criteriaMap.get(fullPath);
    if (existing==null) {
        existing = parentCriteria.createCriteria(pathElement);
        criteriaMap.put(fullPath, existing);
    }
    existing.add(whateverCondition);
}
于 2011-09-23T19:05:53.007 回答
1

我为这个问题找到了更多“好”的解决方案。首先创建一些 searchCriteria 类,例如:

public class SearchCriteria {

    @SearchCriteria(path = "someclass.someclass.id")
    private Integer someId;

    public Integer getSomeId() {
        return someId;
    }

    public void setSomeId(Integer someId) {
        this.someId= someId;
    }
}

和自定义注释:

@Target(value = { ElementType.FIELD })
@Retention(RUNTIME)
public @interface SearchCriteria {
    String path() default "";

    boolean optional() default true;
}

在 DAO 类中:

@Override
    public Collection<SomeClass> find(SearchCriteria criteria) {
        DetachedCriteria hibernateCriteria = forClass(SomeClass.class);

        for (Field field : criteria.getClass().getDeclaredFields()) {
            Annotation annotation = field.getAnnotation(SearchCriteria.class);

            if (annotation == null) {
                continue;
            }

            List<String> elements = Arrays.asList(StringUtils.split(((SearchCriteria) annotation).path(), "."));

            field.setAccessible(true);
            Object fieldValue = null;
            try {
                fieldValue = field.get(criteria);
            } catch (IllegalArgumentException e) {
                LOG.error("while trying to get private field value of entity " + SearchCriteria.class, e);
            } catch (IllegalAccessException e) {
                LOG.error("while trying to get private field value of entity " + SearchCriteria.class, e);
            }

            for (String element : elements) {
                if (elements.indexOf(element) == elements.size() - 1) {
                    hibernateCriteria = hibernateCriteria.add(eq(element, fieldValue));
                } else {
                    hibernateCriteria = hibernateCriteria.createCriteria(element);
                }
            }
        }

        return getHibernateTemplate().findByCriteria(hibernateCriteria);
    }
于 2012-01-20T15:11:35.123 回答