hibernate一对多关联查询为null
如题,myeclipse+hibernate+sql server2008调试的时候出了些问题,蛮久了。
数据库表,t_student(sid,bid,sname)t_banji(bid,room) 关系是一个班级有多个学生。没有设置主键
po.Student ,po.Banji Banji :private String bid;
private String room; private Set students; Student :private String sid; private String bid;
private String sname; private Banji banji; 对应属性有get/set函数。映射文件Banji.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="po.Banji" table="t_banji">
<id name="bid" column="bid">
<generator class="assigned"/>
</id>
<property name="room" column="room"/>
<set name="students"
table="t_student"
lazy="false"
cascade="all"
inverse="true"
>
<key column="bid"></key>
<one-to-many class="po.Student"/>
</set>
</class>
</hibernate-mapping>
映射文件Sutdent.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="po.Student" table="t_student">
<id name="sid" column="sid">
<generator class="assigned"/>
</id>
<property name="bid" column="bid"/>
<property name="sname" column="sname"/>
<many-to-one name="banji" class="po.Banji" insert="false"
update="false" cascade="none" column="bid"/>
</class>
</hibernate-mapping>
以上是配置,现在是测试, 查询班级得到对应的学生,代码如下:
public class One2Many {
public static void main(String []args)
{
Session session=util.HibernateSessionFactory.getSession();
//查询班级知道对应的学生
Banji banji=(Banji)session.get(Banji.class, "11001");
Set set=banji.getStudents();
Iterator ite=set.iterator();
System.out.println(banji.getBid());
if(!ite.hasNext())
{
System.out.println("null");
}
while(ite.hasNext())
{
Student stu=(Student)ite.next();
System.out.println(stu.getSname());
}
util.HibernateSessionFactory.closeSession();
}
}
运行:结果是.11001 null
底层SQL语句是:Hibernate: select banji0_.bid as bid0_0_, banji0_.room as room0_0_ from t_banji banji0_ where banji0_.bid=?
Hibernate: select students0_.bid as bid1_, students0_.sid as sid1_, students0_.sid as sid1_0_, students0_.bid as bid1_0_, students0_.sname as sname1_0_ from t_student students0_ where students0_.bid=?
问题是:查询班级,对应学生显示不出来。请教各大神原因。
免责声明: 本文仅代表作者个人观点,与爱易网无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。