0

我有以下内容:

@Entity
public class Person {

    @Id
    private long id;

    @Column
    private String firstName;

    // getters and setters
}

我希望以下工作:

Criteria criteria = session.createCriteria(Person.class);

criteria.add(Restrictions.eq("firstName", "john"));

OR     

criteria.add(Restrictions.eq("FIRSTNAME", "john"));

OR 

criteria.add(Restrictions.eq("fiRstName", "john"));

基本上,我有一个允许过滤后端对象的 Web 服务,我们试图忽略属性名称中的大小写。我不知道该怎么做。我搜索并查看了休眠文档和休眠源,但一无所获。Hibernate 的例外是

org.hibernate.QueryException: could not resolve property FIRSTNAME (or fiRstName) of com.abc.Person
at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:81)
    at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:75)
    at org.hibernate.persister.entity.AbstractEntityPersister.getSubclassPropertyTableNumber(AbstractEntityPersister.java:1482)
    at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:62)
    at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1457)
    at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:483)
    at org.hibernate.loader.criteria.CriteriaQueryTranslator.findColumns(CriteriaQueryTranslator.java:498)
    at org.hibernate.criterion.SimpleExpression.toSqlString(SimpleExpression.java:68)
4

1 回答 1

0
  • 您可以尝试MetaModel为实体生成类。

    criteria.add(Restrictions.eq(Person_.firstName, "john"));

  • 否则,通常,您可以尝试使用反射获取类中声明的字段。

    Field[] fields = Person.class.getDeclaredFields();      
    
     for(int i = 0; i < fields.length; i++){
    
        if(fields[i].getName().equalsIgnoreCase("FIRSTNAME")){
    
               criteria.add(Restrictions.eq(fields[i].getName(), "john"));      
    
               break;
        }
      }
    
于 2013-04-08T17:25:10.343 回答