ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rick <ric...@gmail.com>
Subject Re: 2 Way 1-1 relationship
Date Fri, 23 Jan 2009 12:55:00 GMT
On Wed, Jan 21, 2009 at 12:34 PM, Petr V. <greatman787@yahoo.com> wrote:
> Thanks Ingmar Lötzsch for the reply.
> Actually I was simplifying the context from 1-M to 1-1 for discussion so left groupBy
attribute mistakenly.
> The problem at hand is that Teacher has students (1:M). Student must have some teacher
assigned to him but teacher does not necessarily need to have some student.

I think you can do what you want by just making another
TeacherResultMap that doesn't include the collection of students.
Actually make that one without the collection first, and then just
extend it as a second map that does have the collection. You'd
probably then have to alias certain column names twice though for
teacher, as I don't think ibatis will map the same column name to
multiple result maps (I could be wrong there though.) But all that
being said, i'd have to ask why you would ever need this relationship
in one query?

You mention you also want the student information to have 'teacher'
information, but from a code standpoint wherever you are using your
list of teachers, you'll have that info available so do you really
need that info populated in the student as well?

I understand a result map that extends student that has a property for
"Teacher' information, for cases where you are viewing information on
a single student - but that would be a separate call usually for when
you really need that info about a teacher in a student. For when
selecting a single student.

I'd be curious from a UI or business standpoint what are you doing
where you'd need each student fully populated with teacher information
when viewing/using a List that all the teachers at your disposal.

For example in a real app, you might be displaying a list of teachers
and the students beneath each teacher. You click on a student to get
more detail on the student. At that point you can get the teacher info
if you really need it. I think that would be cleaner anyway. (Or if
you wanted pass the teacher record which you have along with the
student record in the call - obviously in a web app you probably
wouldn't do this and would be passing an "studentID" - at that point
I'd just use that studentID to get me a fully hydrated student -
possibly with the teacher info in the student if needed as well.)

If you really wanted what you were asking for maybe something like...

<resultMap id="TeacherResultMap" class="Teacher" groupBy="id" >
    <result property="id" />
    <result property="name" />
    <result property="students" resultMap="JOB.StudentResultMap"/>

<resultMap id="StudentResultMap" class="Student" >
  <result property="id" column="stu_id"/>
  <result property="name" column="stu_Name"/>
  <result property="teacher.id" column="studentTeacherID" />
  <result property="teacher.name" columns="studenTeacherName" />

<select id="findTeachersAndStudents"  parameterClass="int"
            t.id as studentTeacherID,
            t.name ,
            t.name as studentTeacherName,
             s.id as stu_id, s.name as stu_Name , s.tid as stu_tid
            from Teacher t left join student s on t.id = s.tid where
t.id = #value#

View raw message