hibernate 一对多的问题
我有三张表 university, college, professional 。
关系是:
university ----- college college -----professional
1 -------- N 1 ------ N
现在前台传来一个参数“100010204”,我将这个字符串割开后得到三个参数uniNum(10001), collNum(02), profNum(04)
一对多都是单向的,只能从1的一端访问,映射没有任何问题,现在我要查询university对象,需要得到这个university的一个college和college下的一个professional
代码如下
Java code
private University getUnivercity(String will) throws Exception {
//取志愿代码的前五位作为学校代码
String uniNum = will.substring(0,5);
//取志愿代码的6-7位作为学院代码
String collNum = will.substring(5,7);
//取志愿代码的最后两位最为专业代码
String perNum = will.substring(7,9);
University univer = (University) getHibernateTemplate().find("from University as u inner join fetch u.college coll,u.college.professional prof " +
" where " +
"u.univerNum="+uniNum+" and coll.collNum="+collNum+" and prof.professNum="+perNum).get(0);
//+" and prof.professNum="+perNum);
System.out.println(univer.getCollege());
return univer;
结果报错:
Struts Problem Report
Struts has detected an unhandled exception:
Messages: u.college.professional is not mapped [from alex.ssh.ems.vo.University as u inner join fetch u.college coll,u.college.professional prof where u.univerNum=10001 and coll.collNum=02 and prof.professNum=04]
u.college.professional is not mapped [from alex.ssh.ems.vo.University as u inner join fetch u.college coll,u.college.professional prof where u.univerNum=10001 and coll.collNum=02 and prof.professNum=04];
nested exception is org.hibernate.hql.ast.QuerySyntaxException: u.college.professional is not mapped [from alex.ssh.ems.vo.University as u inner join fetch u.college coll,u.college.professional prof where u.univerNum=10001 and coll.collNum=02 and prof.professNum=04]
File: org/hibernate/hql/ast/util/SessionFactoryHelper.java
Line number: 180
只抓取college是没有问题的,同时要将college下的指定professional抓出来我就不知道怎么弄了,大侠们帮忙啊!!!
------解决方案--------------------不允许从集合再往下级联
u.college已经是一个集合了,再u.college.professional是错误的。
------解决方案--------------------可以用纯SQL,或者把u.college.professional改成coll.professional试试。