hibernate 内外连接查询
时间: 2023-10-20 11:07:35 浏览: 58
在 Hibernate 中,可以使用 HQL 或者 Criteria API 来进行内外连接查询。
1. 内连接查询
内连接查询可以使用 HQL 或者 Criteria API 来实现。以下是 HQL 的示例:
```java
String hql = "select c.name, o.orderNumber from Customer c inner join c.orders o where c.name like '%John%'";
Query query = session.createQuery(hql);
List<Object[]> results = query.list();
for(Object[] row : results) {
String customerName = (String) row[0];
String orderNumber = (String) row[1];
// do something with the result
}
```
以上 HQL 查询使用了 `inner join` 关键字。在这里,我们查询了所有名字包含 "John" 的客户及其所有订单号。
以下是 Criteria API 的示例:
```java
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<Object[]> query = builder.createQuery(Object[].class);
Root<Customer> customer = query.from(Customer.class);
Join<Customer, Order> order = customer.join("orders", JoinType.INNER);
query.multiselect(customer.get("name"), order.get("orderNumber"));
query.where(builder.like(customer.get("name"), "%John%"));
List<Object[]> results = session.createQuery(query).getResultList();
for(Object[] row : results) {
String customerName = (String) row[0];
String orderNumber = (String) row[1];
// do something with the result
}
```
以上 Criteria API 查询使用了 `join` 方法,并指定了 `JoinType.INNER` 来进行内连接查询。在这里,我们查询了所有名字包含 "John" 的客户及其所有订单号。
2. 外连接查询
外连接查询同样可以使用 HQL 或者 Criteria API 来实现。以下是 HQL 的示例:
```java
String hql = "select c.name, o.orderNumber from Customer c left join c.orders o";
Query query = session.createQuery(hql);
List<Object[]> results = query.list();
for(Object[] row : results) {
String customerName = (String) row[0];
String orderNumber = (String) row[1];
// do something with the result
}
```
以上 HQL 查询使用了 `left join` 关键字。在这里,我们查询了所有客户及其所有订单号,如果客户没有订单则订单号为 null。
以下是 Criteria API 的示例:
```java
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<Object[]> query = builder.createQuery(Object[].class);
Root<Customer> customer = query.from(Customer.class);
Join<Customer, Order> order = customer.join("orders", JoinType.LEFT);
query.multiselect(customer.get("name"), order.get("orderNumber"));
List<Object[]> results = session.createQuery(query).getResultList();
for(Object[] row : results) {
String customerName = (String) row[0];
String orderNumber = (String) row[1];
// do something with the result
}
```
以上 Criteria API 查询同样使用了 `join` 方法,并指定了 `JoinType.LEFT` 来进行左外连接查询。在这里,我们查询了所有客户及其所有订单号,如果客户没有订单则订单号为 null。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)