当使用JPA进行数据访问时,可能会遇到N+1问题。N+1问题是指在查询关联实体时,JPA会执行额外的N+1次查询,其中N是关联实体的数量。 解决方案:使用JPA2.1特性 @NamedEntityGraph注解,这是JPA推出专门优化解决JPA效率的注解。不管是lazy还是eager,在读取数据的时候,都会有N+1问题。而EntityGraph则直接在查询语句的时候,直接用到用到Left Join,优化了数据库的性能。@NamedEntityGraph注解可以应用于实体类的属性上,用于定义关联实体的抓取策略。通过在查询中使用这个注解,可以一次性获取关联实体的数据,避免额外的查询。
以下是一个使用@NamedEntityGraph注解解决N+1问题的示例代码: 我们先定义实体类Customer,Order,其中Customer和Order之间是一对多的关系。
-
定义实体类
@NamedEntityGraph(
name = "customer-with-orders",
attributeNodes = {
@NamedAttributeNode("orders")
})
@Entity
public class Customer {
@Id
private Integer customerNumber;
@OneToMany(mappedBy = "customer", fetch = FetchType.LAZY)
private Set<Order> orders;
}
在上述代码中,@EntityGraph注解指定了要使用的命名实体图"customer-with-orders",指定加载关联实体orders。这样就可以在查询客户列表时一次性获取关联的订单信息,避免N+1问题。也可以配置视图的子视图,如下所示,Order类中关联了Orderdetail实体。
@Entity
public class Order {
@OneToMany(fetch = FetchType.LAZY, mappedBy = "order")
private Set<OrderDetail> orderDetail;
}
@NamedEntityGraph(
name = "customer-with-orders-and-details",
attributeNodes = {
@NamedAttributeNode(value = "orders", subgraph = "order-details")
},
subgraphs = {@NamedSubgraph(
name = "order-details",
attributeNodes = {
@NamedAttributeNode(value = "orderDetail")
}
)}
)
public class Customer {
@Id
private Integer customerNumber;
@OneToMany(mappedBy = "customer", fetch = FetchType.LAZY)
private Set<Order> orders;
}
上述代码中,@NamedAttributeNode指定要加载的关联属性orders,和orders的关联实体orderDetail。查询时便可将orders属性和orders属性关联的orderDetail一并查出。
-
使用视图
方法一:在接口中的方法上加@EntityGraph注解
@Repository
public interface CustomerRepository extends JpaRepository<Customer, Integer> ,BaseRepository<Customer,Integer>{
@EntityGraph(value = "customer-with-orders",type = EntityGraph.EntityGraphType.FETCH)
Customer findByCustomerName(String customerName);
}
value属性指定使用的视图名称,对应实体类Customer上@NamedEntityGraph注解中的name属性,EntityGraph.EntityGraphType.FETCH是一个枚举类型,用于指定实体图的加载方式。FETCH表示使用立即加载的方式加载实体图。在使用此方式加载实体图时,相关的属性将会立即从数据库中加载,并与查询结果一同返回,LOAD表示使用延迟加载的方式获取实体及其关联的实体。
public static enum EntityGraphType {
LOAD("jakarta.persistence.loadgraph"),
FETCH("jakarta.persistence.fetchgraph");
private final String key;
private EntityGraphType(String value) {
this.key = value;
}
public String getKey() {
return this.key;
}
}
}
方法二:自定义实现类使用entityManager.getEntityGraph(graphName)方法动态获取视图。
@Repository
public class CustomerRepositoryImpl implements BaseRepository<Customer, Integer> {
@PersistenceContext
private EntityManager entityManager;
@Override
public Customer findWithGraph(Integer id, String graphName) {
EntityGraph entityGraph = entityManager.getEntityGraph(graphName);
Map<String, Object> properties = new HashMap<>();
properties.put("javax.persistence.fetchgraph", entityGraph);
return entityManager.find(Customer.class, id, properties);
}
}
entityManager.getEntityGraph是一个用于获取实体图的方法。在给定一个实体类型和一个图名称的情况下,它返回与该图关联的EntityGraph对象,该方法所需的参数就是我们定义的视图名称。此方法可将视图名称作为参数传入,动态获取视图,较为灵活。其中properties中的key对应第一种使用方法中的枚举值EntityGraph.EntityGraphType.FETCH。
-
方法调用
public CustomerWithOrdersDto getCustomerAndOrders(Integer id,String graphName) {
Customer customer = customerRepository.findWithGraph(id, graphName);
CustomerWithOrdersDto customerWithOrders = customerMapper.customerToCustomerWithOrdersDto(customer);
List<OrderDto> orders = customer.getOrders().stream()
.map(order -> orderMapper.OrderToOrderDto(order))
.collect(Collectors.toList());
customerWithOrders.setOrders(orders);
return customerWithOrders;
}
将主键(id)和视图名称(graphName)作为参数传入刚才定义的方法之中,实现一次性加载出视图中配置的所需节点数据。
其中customerMapper.customerToCustomerWithOrdersDto(Customer customer)是一个将Customer类型转换为CustomerWithOrdersDto类型的bean拷贝工具,需要注意的是拷贝过程中可能会触发已查询出来的实体中的懒加载。所以做如下配置忽略orders的拷贝以防止触发懒加载。
@Mapper(componentModel = "spring")
public interface CustomerMapper {
@Mapping(target = "orders",ignore = true)
CustomerWithOrdersDto customerToCustomerWithOrdersDto(Customer customer);
}