连接查询

package myHibernate;
import java.util.Iterator;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import junit.framework.TestCase;
/**
 * 连接查询
 * 2010年4月14日 21:39:56
 * */
public class JoinQuery extends TestCase {
 public void testJoinQuery_1() {
  Session session = null;
  Transaction tx = null;
  try {
   session = HibernateUtils.getSession();
   tx = session.beginTransaction();
   //右连接
  // Query query  = session.createQuery("select s.name,c.name from Student s right join s.classes c");  
   //左连接
   Query query  = session.createQuery("select s.name,c.name from Student s left join s.classes c");
  //默认为iner join ,iner可以不显式加上
  // Query query  = session.createQuery("select s.name,c.name from Student s  join s.classes c");
   Iterator iter = query.list().iterator();
   while(iter.hasNext()){
    Object obj[] = (Object[])iter.next();
    System.out.println(obj[0]+" "+obj[1]);
   }  
   tx.commit();
  } catch (HibernateException e) {
   e.printStackTrace();
   if (tx != null)
    tx.rollback();
  } finally {
   HibernateUtils.closeSession(session);
  }
 }
}
 

版权声明:本文为博主原创文章,未经博主允许不得转载。

你可能感兴趣的:(JOIN,session,object,null,iterator,query)