我是hibernate和Spring-data-jpa的新手。我想要做的就是使用分页来获取实体及其所有相关实体(大约有4千万条记录)。即一个查询以急切获取根实体及其所有映射实体/集合= 1查询
但我遇到(n + 1)问题:一个查询根实体+一个查询相关的映射实体/每个根实体的集合=(n + 1)个查询 这实际上影响了性能。
我还想知道我在实体中的映射是否正确。
有人可以指导我。感谢
应用程序堆栈:
<java-version>1.7</java-version>
<spring.framework.version>4.0.2.RELEASE</spring.framework.version>
<hibernate-core>4.3.4.Final</hibernate-core>
<hibernate-validator>5.1.0.Final</hibernate-validator>
<hibernate-entitymanager>4.3.4.Final</hibernate-entitymanager>
<spring-data-jpa>1.5.1.RELEASE</spring-data-jpa>
我有2个实体类(Customer和Order)如下:
客户实体
@Entity
@Table(name = "view_customerOrders_to_process")
public class Customer implements Serializable {
private Long id;
private List<Order> Orders;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "customer_id", updatable = false)
public Long getId() {
return id;
}
........
@NotFound(action = NotFoundAction.IGNORE)
@OneToMany(fetch = FetchType.EAGER, mappedBy = "customer")
//Do not cascade any changes to child entities.
@Where(clause = "order_status = 'PENDING'")
public List<Order> getOrders() {
return order;
}
}
尝试使用batchSize,但由于某种原因它不起作用(相同的n + 1问题)
//@Fetch(FetchMode.SELECT)
// @ BatchSize(size = 25)
订单实体:
@Table(name = "tbl_orders")
public class Order implements Serializable {
private Long id;
private int customerId;
private Customer customer;
private OrderType orderType;
..........
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "order_id", updatable = false)
public Long getId() {
return this.id;
}
@OneToOne
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(name = "order_type", nullable = true, unique = false, updatable = false)
public OrderType getOrderType() {
return this.orderType;
}
@NotFound(action = NotFoundAction.IGNORE)
@ManyToOne(fetch = FetchType.LAZY, optional = true, targetEntity = Customer.class)
@Fetch(FetchMode.JOIN)
@JoinColumns({
@JoinColumn(updatable = false, insertable = false,
name="customer_id", referencedColumnName = "customer_id", nullable=false),
. . . . .
})
public Customer getCustomer() {
return customer;
}
}
服务类:
@Service
public class MetsGeneratorService {
. . .
@Autowired
private CustomerRepository customerRepository;
public List<Customer> run() {
PageRequest pageRequest = new PageRequest(0, batchSize);
List<Customer> customers = customerRepository.findOrdersUnprocessed(pageRequest);
return customers;
}
. . .
}
存储库:
public interface CustomerRepository extends JpaRepository<Customer, Long> {
@Query("SELECT e FROM Customer e")
List<Customer> findOrdersUnprocessed(Pageable pageable);
//Tried using the below one which seems to eliminate paging
//and understandably getting out of memory.
//@Query(value = "SELECT e FROM Customer e left join fetch e.orders")
}
尝试改为
@Query(value = "SELECT e FROM Customer e left join fetch e.orders",
countQuery = "select count(e) from Customer e")
List<Customer> findOrdersUnprocessed(Pageable pageable);
如果你看到正在处理的日志正在进行&#34;选择*&#34;而不是分页。
日志消息是 WARN org.hibernate.hql.internal.ast.QueryTranslatorImpl - HHH000104:使用collection fetch指定的firstResult / maxResults;在记忆中应用!
答案 0 :(得分:1)
您可以使用
@Fetch(FetchMode.SUBSELECT)
删除(n + 1)查询问题。