当调用session.load()发放返回的实际对象不止一个时应该如何分别取得每个对象的引用?
比如:
数据库如下:
table:Student
Sid Sname Sgroup
1 yang 5
2 yue 5
table :Sgroup
id name
5 ball
其中Student表中的Sgroup字段是Group表中的外键
Hibernate:
select
group0_.ID as ID1_1_,
group0_.NAME as NAME1_1_,
stu1_.SID as SID0_0_,
stu1_.SGROUP as SGROUP0_0_,
stu1_.SNAME as SNAME0_0_
from
student.dbo.Sgroup Sgroup0_
left outer join
student.dbo.Student Student1_
on Sgroup0_ .ID=Student1_ .SGROUP
where
Sgroup0_.ID=?
而这条语句的结果是: id name Sid Sname Sgroup
5 ball 1 yang 5
5 ball 2 yue 5
但是session.load()方法返回的是一个对象,虽然可以通过转型使其为Sgroup类型,但是在引用其值的时候就会抛出异常,
比如:
group = (Sgroup) session.load(Sgroup.class, 1);
...........................
System.out.println(group.getName());
产生如下异常
More than one row with the given identifier was found
对于这种情况应该如何引用所返回的两个Sgroup的两个对象呢?
比如:
数据库如下:
table:Student
Sid Sname Sgroup
1 yang 5
2 yue 5
table :Sgroup
id name
5 ball
其中Student表中的Sgroup字段是Group表中的外键
Hibernate:
select
group0_.ID as ID1_1_,
group0_.NAME as NAME1_1_,
stu1_.SID as SID0_0_,
stu1_.SGROUP as SGROUP0_0_,
stu1_.SNAME as SNAME0_0_
from
student.dbo.Sgroup Sgroup0_
left outer join
student.dbo.Student Student1_
on Sgroup0_ .ID=Student1_ .SGROUP
where
Sgroup0_.ID=?
而这条语句的结果是: id name Sid Sname Sgroup
5 ball 1 yang 5
5 ball 2 yue 5
但是session.load()方法返回的是一个对象,虽然可以通过转型使其为Sgroup类型,但是在引用其值的时候就会抛出异常,
比如:
group = (Sgroup) session.load(Sgroup.class, 1);
...........................
System.out.println(group.getName());
产生如下异常
More than one row with the given identifier was found
对于这种情况应该如何引用所返回的两个Sgroup的两个对象呢?