hibernate中表怎么根据外键查询
我有两张表:Teacherid(主键)nameStudentid(主键)nametid(外键对应Teacher的id)publicListfindStudentByTea...
我有两张表: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> 展开
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> 展开
2个回答
展开全部
改为:String HQL = "select s.name from Student as s where s.teacher.id ="+teacher.getId();
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询