多表操作测试类
public class HQLJoinTest {
@Test
public void test1() {
Session session = HibernateUtils.openSession();
session.beginTransaction();
String hql = "from Customer c inner join c.orders with c.id = 1";
Query query = session.createQuery(hql);
List<Object[]> list = query.list();
for (Object[] objects : list) {
for (Object object : objects) {
System.out.print(object+"\t");
}
}
session.getTransaction().commit();
session.close();
}
@Test
public void test2() {
Session session = HibernateUtils.openSession();
session.beginTransaction();
String hql = "from Order o where o.c.id = 1 ";
Query createQuery = session.createQuery(hql);
List list = createQuery.list();
System.out.println(list);
session.getTransaction().commit();
session.close();
}
@Test
public void test3() {
Session session = HibernateUtils.openSession();
session.beginTransaction();
String hql = "select distinct c from Customer c inner join fetch c.orders where c.id = 1 ";
Query createQuery = session.createQuery(hql);
List<Customer> list = createQuery.list();
for (Customer customer : list) {
System.out.println(customer);
}
session.getTransaction().commit();
session.close();
}
@Test
public void test4() {
Session session = HibernateUtils.openSession();
session.beginTransaction();
String hql = "from Customer c left outer join c.orders";
Query createQuery = session.createQuery(hql);
List<Object[]> list = createQuery.list();
for (Object[] objects : list) {
for (Object object : objects) {
System.out.println(object+"\t");
}
}
session.getTransaction().commit();
session.close();
}
@Test
public void test5() {
Session session = HibernateUtils.openSession();
session.beginTransaction();
String hql = "select distinct c from Customer c left outer join fetch c.orders where c.id = 1";
Query createQuery = session.createQuery(hql);
List<Customer> list = createQuery.list();
for (Customer customer : list) {
System.out.println(customer);
}
session.getTransaction().commit();
session.close();
}
}