休眠动态运算符字符串

时间:2018-11-08 18:33:00

标签: java hibernate hibernate-criteria

在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();
}

可以,但是还有其他方法吗?

谢谢!

0 个答案:

没有答案