You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by jacklin10 <ja...@gmail.com> on 2008/01/25 17:34:13 UTC

Many to many results reversed

I'm trying to demo iBATIS and I can get this many-to-many relationship to
work correctly.  

Mapping file:

<?xml version="1.0" encoding="UTF-8"?>
<!-- <sqlMap namespace="students"> --><!DOCTYPE sqlMap PUBLIC
"-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="customStudents">
  
  <resultMap class="bo.customStudent_many_to_many"
id="abatorgenerated_StudentTeacherResult" groupBy="student_id" >
 
    <result column="student_id"  property="studentId" />
    <result column="first_name"  property="firstName" />
    <result column="last_name"   property="lastName"  />
    <result column="grade"       property="grade"     />
    <result column="sex"         property="sex"       />
    <result column="test"        property="test"      />
    
	<!-- NON GENERATED CODE AHEAD -->
    <result property="teachers" resultMap="customStudents.TeachersList"/>
     
  </resultMap>
                 
  <resultMap class="bo.customTeacher_many_to_many" id="TeachersList">
    
    <result column="teacher_id" property="teacherId" />
    <result column="t_first_name" property="firstName" />
    <result column="t_last_name"  property="lastName"  />
 
  </resultMap>
  
  <select id="find_teacher_by_id"
resultClass="bo.customTeacher_many_to_many"
resultMap="customStudents.TeachersList" >
        SELECT  t.teacher_id, 
        		t.t_first_name , 
        		t.t_last_name   
		FROM teachers t
		WHERE t.teacher_id = #key#
	</select>   

  <select id="find_student_by_id" parameterClass="int"
resultMap="customStudents.abatorgenerated_StudentTeacherResult" >
		SELECT 
			t.teacher_id, 
			t.t_first_name, 
			t.t_last_name,
			s.student_id, 
			s.first_name,
			s.last_name,
			s.grade,
			s.sex,
			s.test
		FROM teachers t
		LEFT JOIN teachers_students st ON st.teacher_id = t.teacher_id
		LEFT JOIN students s ON	s.student_id = st.student_id
		WHERE s.student_id = #pkey#
	</select>   


</sqlMap>

Java Code:
   List studs = client.queryForList("customStudents.find_student_by_id", 1);

Results:

Studs size: 2

Stud dump: {studentId=1, sex=m,
teachers=[bo.customTeacher_many_to_many@2103d4e], class=class
bo.customStudent_many_to_many, grade=100, test=te, firstName=joe,
lastName=acklin}
---> Teach dump: {class=class bo.customTeacher_many_to_many, teacherId=1,
firstName=snooty, lastName=mcSnootson}

Stud dump: {studentId=1, sex=m,
teachers=[bo.customTeacher_many_to_many@2103e23], class=class
bo.customStudent_many_to_many, grade=100, test=te, firstName=joe,
lastName=acklin}
---> Teach dump: {class=class bo.customTeacher_many_to_many, teacherId=2,
firstName=dorky, lastName=Von PeckerStein}

Its giving me two students, and one teacher per student.  The results I want
are 1 student with a list of two Teachers.
I can't figure out what i'm doing wrong here.  

Any suggestions?  
Thanks guys! .. and girls :)






-- 
View this message in context: http://www.nabble.com/Many-to-many-results-reversed-tp15091562p15091562.html
Sent from the iBATIS - User - Java mailing list archive at Nabble.com.