HQL(九)接続クエリー


接続クエリー(重要)
*内部接続
*外部ジョイン(左/右)
参照:JoinQueryTest.java
内部接続は3種類あります:inner join,join,"="

package com.wlh.hibernate;

import java.util.Iterator;
import java.util.List;

import junit.framework.TestCase;

import org.hibernate.Session;

public class JoinQueryTest extends TestCase{
	
	/**
	 *    
	 */
	public void testQuery1() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			 //====   join==========//
			List students=session.createQuery("select s.name ,c.name from Student s join s.classes c").list();
			for(Iterator iter=students.iterator();iter.hasNext();){
				Object [] o=(Object [])iter.next();
				System.out.println("id="+o[0]+",o[1]"+o[0]);
			}
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}	
	
	/**
	 *    
	 */
	public void testQuery2() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			 //=======   inner join        ,  inner    ,     ======//
			List students=session.createQuery("select s.name, c.name from Student s inner join s.classes c").list();
			for(Iterator iter=students.iterator();iter.hasNext();){
				Object [] o=(Object [])iter.next();
				System.out.println("id="+o[0]+",o[1]"+o[0]);
			}
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}	
	
	/**
	 *    
	 */
	public void testQuery3() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			//=========== "="    ===========//
			List students=session.createQuery("select s.name ,c.name from Student s ,Classes c where s.classes.id=c.id").list();
			for(Iterator iter=students.iterator();iter.hasNext();){
				Object [] o=(Object [])iter.next();
				System.out.println("id="+o[0]+",o[1]"+o[0]);
			}
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}	
	

[b][color=red]   :    2            [/color]	[/b]
         


 
    /**
*
*/
public void testQuery4() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
// ( )
List students=session.createQuery("select s.name ,c.name from Student s  left join s.classes c").list();
for(Iterator iter=students.iterator();iter.hasNext();){
Object [] o=(Object [])iter.next();
System.out.println("id="+o[0]+",o[1]="+o[1]);
}
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}



/**
*
*/
public void testQuery5() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
// ,( )
List students=session.createQuery("select s.name ,c.name from Student s  right join s.classes c").list();
for(Iterator iter=students.iterator();iter.hasNext();){
Object [] o=(Object [])iter.next();
System.out.println("id="+o[0]+",o[1]="+o[1]);
}
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}


}