我有以下内容:
@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文档和Hibernate源代码,但一无所获。 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)
答案 0 :(得分: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;
}
}