Java 获取休眠异常:org.hibernate.QueryException:无法解析属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1892587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting Hibernate Exception : org.hibernate.QueryException: could not resolve property
提问by Nirmal
I have one table with composite key attribute..
我有一张带有复合键属性的表..
For that i have made 2 beans for hibernate annotations mappings..
为此,我为 hibernate 注释映射制作了 2 个 bean。
Now, it works fine for save, update and delete..
现在,它可以很好地保存、更新和删除..
But when I am fetching using some criteria, it's giving me "could not resolve property" exception.
但是当我使用某些标准进行提取时,它给了我“无法解析属性”的异常。
My Beans are as follows :
我的豆子如下:
WBList.java
WBList.java
@Entity
public class WBList {
private WBListPK id;
private String wb;
@Id
public WBListPK getId() {
return id;
}
public void setId(WBListPK id) {
this.id = id;
}
@Column(name = "wb")
public String getWb() {
return wb;
}
public void setWb(String wb) {
this.wb = wb;
}
}
WBListPK.java
WBListPK.java
@Embeddable
public class WBListPK implements Serializable {
private int rid;
private int sid;
public WBListPK() {
}
public WBListPK(Integer rid, Integer sid) {
this.rid = rid;
this.sid = sid;
}
public int getRid() {
return rid;
}
public void setRid(int rid) {
this.rid = rid;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
}
FindByAll Method of My DAO is as follows :
我的 DAO 的 FindByAll 方法如下:
public List<WBList> findByAll(final WBListPK wbListPK, final String wb) {
List results = null;
results = this.hibernateTemplate.executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Criteria criteria = session.createCriteria(WBList.class);
if (wb != null) {
criteria.add(Expression.like("wb", wb));
}
if(wbListPK.getRid()!=0){
criteria.add(Expression.eq("rid", wbListPK.getRid()));
}
if(wbListPK.getSid()!=0){
criteria.add(Expression.eq("sid", wbListPK.getSid()));
}
return criteria.list();
}
});
return results;
}
I am calling this findByAll method from my controller, the code is :
我从我的控制器调用这个 findByAll 方法,代码是:
WBListPK wbListPK = new WBListPK();
WBList wbList = new WBList();
wbListPK.setRid(10);
wbListPK.setSid(20);
List<WBList> wbListList = this.wbListSecurityProcessor.findByAll(wbListPK, "b");
System.out.println("wbListList = "+wbListList);
When I am executing above code, it's giving me following exception (with stacktrace):
当我执行上面的代码时,它给了我以下异常(使用堆栈跟踪):
org.springframework.orm.hibernate3.HibernateQueryException: could not resolve property: rid of: com.sufalam.mailserver.bean.WBList; nested exception is org.hibernate.QueryException: could not resolve property: rid of: com.sufalam.mailserver.bean.WBList
org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:655)
org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)
org.springframework.orm.hibernate3.HibernateTemplate.executeFind(HibernateTemplate.java:343)
com.sufalam.mailserver.dao.WBListDao.findByAll(WBListDao.java:42)
com.sufalam.mailserver.business.WBListProcessor.findByAll(WBListProcessor.java:33)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:196)
$Proxy113.findByAll(Unknown Source)
com.sufalam.mailserver.business.security.WBListSecurityProcessor.findByAll(WBListSecurityProcessor.java:28)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:196)
$Proxy120.findByAll(Unknown Source)
com.sufalam.mailserver.presentation.web.WBListManageController.handleRequest(WBListManageController.java:65)
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:781)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:726)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:636)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:545)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
root cause
org.hibernate.QueryException: could not resolve property: rid of: com.sufalam.mailserver.bean.WBList
org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:44)
org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:38)
org.hibernate.persister.entity.AbstractEntityPersister.getSubclassPropertyTableNumber(AbstractEntityPersister.java:1379)
org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:31)
org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1354)
org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:434)
org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumnsUsingProjection(CriteriaQueryTranslator.java:394)
org.hibernate.criterion.SimpleExpression.toSqlString(SimpleExpression.java:45)
org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:334)
org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:90)
org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:59)
org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:69)
org.hibernate.impl.SessionImpl.list(SessionImpl.java:1554)
org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
com.sufalam.mailserver.dao.WBListDao.doInHibernate(WBListDao.java:59)
org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
org.springframework.orm.hibernate3.HibernateTemplate.executeFind(HibernateTemplate.java:343)
com.sufalam.mailserver.dao.WBListDao.findByAll(WBListDao.java:42)
com.sufalam.mailserver.business.WBListProcessor.findByAll(WBListProcessor.java:33)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:196)
$Proxy113.findByAll(Unknown Source)
com.sufalam.mailserver.business.security.WBListSecurityProcessor.findByAll(WBListSecurityProcessor.java:28)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:196)
$Proxy120.findByAll(Unknown Source)
com.sufalam.mailserver.presentation.web.WBListManageController.handleRequest(WBListManageController.java:65)
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:781)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:726)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:636)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:545)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
Please help me if anybody have any solutions..
如果有人有任何解决方案,请帮助我..
Thanks in advance..
提前致谢..
采纳答案by ChssPly76
rid
and sid
are properties of composite identifier (WbListPK
), not the entity itself. You, therefore, need to refer to them accordingly:
rid
和sid
是复合标识符 ( WbListPK
) 的属性,而不是实体本身。因此,您需要相应地引用它们:
if(wbListPK.getRid()!=0){
criteria.add(Expression.eq("id.rid", wbListPK.getRid()));
}
if(wbListPK.getSid()!=0){
criteria.add(Expression.eq("id.sid", wbListPK.getSid()));
}
Note the id.
prefix. See where clauseand referring to id propertychapters in Hibernate documentation for more details / examples (they deal with HQL but majority of stuff applies to Criteria API as well)
注意id.
前缀。有关更多详细信息/示例,请参阅where 子句并参考Hibernate 文档中的id 属性章节(它们处理 HQL,但大部分内容也适用于 Criteria API)
回答by Arturas M
Well not really a specific answer to this question, but might help other people who get an error like this, for me it was about case sensitivity. In other words, the string property name in the criteria you give has to match the one in your mapper file, for instance:
好吧,这不是这个问题的具体答案,但可能会帮助其他遇到此类错误的人,对我来说,这与区分大小写有关。换句话说,您提供的条件中的字符串属性名称必须与映射器文件中的匹配,例如:
criteria.add(Restrictions.eq("patient", patient));
I had the error since I had used "Patient" here, while my entity has the property "Patient" and the database table has the field "idPatient", while I had named the relation in mapper file as "patient". Just posting, maybe this will help some people in the future, because I got into this thread by inputting the error in google.
因为我在这里使用了“患者”,所以我遇到了错误,而我的实体具有属性“患者”并且数据库表具有字段“idPatient”,而我将映射器文件中的关系命名为“患者”。只是张贴,也许这会在将来对某些人有所帮助,因为我通过在谷歌中输入错误进入了这个线程。