0

有没有办法在 Hibernate 中使用运算符,你不知道提前的类型?这是上下文的一些示例代码,展示了我希望它如何工作:

s.createQuery("select from :table where column :op :value");

这不起作用,所以我写了这个switch逻辑:

public List<Model> findByProperty(final String propertyName,
                                  final String operator,
                                  final String value) {
  final Session s = getSession();
  Criteria criteria = s.createCriteria(Foo.class);
  // How could I dynamically use the type of operator
  // through Hibernate without perhaps a switch on operator

  switch (operator) {
    case '=':
      criteria = criteria.add(Expression.eq(propertyName, value);
    // ...
  }

  return criteria.list();
}

这行得通,但还有其他方法吗?

谢谢!

4

0 回答 0