HQL多表查询语法(不常用)
程序员文章站
2022-04-15 22:53:28
...
import java.util.Arrays;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.pojo.Customer;
import com.utils.HibernateUtils;
public class Demo_Join {
public static void main(String[] args) {
//fun1();
//fun2();
//fun3();
//fun4();
fun5();
}
//HQL 内连接 => 将连接的两端对象分别返回.放到数组中.
public static void fun1() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
String hql = "from Customer c inner join c.linkMans";
Query query = session.createQuery(hql);
List<Object[]> list = query.list();
for (Object[] obj : list) {
System.out.println(Arrays.toString(obj));
}
tx.commit();
session.close();
}
//HQL 迫切内连接 => 帮我们进行封装.返回值就是一个对象
public static void fun2() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
String hql = "from Customer c inner join fetch c.linkMans";
Query query = session.createQuery(hql);
List<Customer> list = query.list();
System.out.println(list);
tx.commit();
session.close();
}
//HQL 左外连接 => 将连接的两端对象分别返回.放到数组中.
public static void fun3() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
String hql = "from Customer c left join c.linkMans";
Query query = session.createQuery(hql);
List<Object[]> list = query.list();
for (Object[] obj : list) {
System.out.println(Arrays.toString(obj));
}
tx.commit();
session.close();
}
//HQL 右外连接 => 将连接的两端对象分别返回.放到数组中.
public static void fun4() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
String hql = "from Customer c right join c.linkMans";
Query query = session.createQuery(hql);
List<Object[]> list = query.list();
for (Object[] obj : list) {
System.out.println(Arrays.toString(obj));
}
tx.commit();
session.close();
}
//HQL 迫切左外连接 => 帮我们进行封装.返回值就是一个对象
public static void fun5() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
String hql = "from Customer c left join fetch c.linkMans";
Query query = session.createQuery(hql);
List<Customer> list = query.list();
System.out.println(list);
tx.commit();
session.close();
}
}
上一篇: Hql查询