org.hibernate.QueryException: could not resolve property:xxxxxid 外來鍵查詢 hibernate
阿新 • • 發佈:2019-01-25
轉載:http://zhidao.baidu.com/link?url=IqxAQMkf0qRbYNqKKmjLLZ_zSZOQf-RGJ5lmokc04VDLDQJWS1MP14lFnevGB2rCS0WtifXaAAcqRmEqNY8tHq
我有兩張表:Teacher id(主鍵) name Student id(主鍵) name tid(外來鍵對應Teacher的id) public List findStudentByTeacher(Teacher teacher) { try { session = this.openSession(); String HQL = "select s.name from Student as s where s.tid="+teacher.getId(); query = session.createQuery(HQL); return query.list(); } catch (Exception e) { e.printStackTrace(); logs.error("查詢學生時候出現錯誤!"); return null; }finally{ this.closeSession(session); } } 呼叫時出現異常: org.hibernate.QueryException: could not resolve property: tid of: com.accp.hibernate.po.Student [select s.name from com.accp.hibernate.po.Student as s where s.tid =2] at org.hibernate.persister.entity.AbstractPropertyMapping.throwPropertyException(AbstractPropertyMapping.java:43) at ... ... 2008-11-04 16:57:10,203 [com.accp.hibernate.dao.BusinessDAOimpt]-[ERROR] 查詢學生時候出現錯誤! 我tid為2的學生有很多 為什麼?? 請問要配置什麼?還是HQL語句寫的有問題?
能改嗎? 在那裡能改? 是 <many-to-one name="teacher" class="com.accp.hibernate.po.Teacher" fetch="select"> <column name="tid" not-null="true" /> </many-to-one>
改為:String HQL = "select s.name from Student as s where s.teacher.id ="+teacher.getId();