双向多对多关系中子/父实体的有效获取

时间:2018-10-01 12:25:34

标签: java mysql spring-boot many-to-many lazy-loading

我正在开发一个使用mysql数据库的SpringBoot服务。

我的数据库中有三个表:人员,收件人和类别。人员与类别具有双向的多对多关系,收件人也是如此。集合会延迟加载。

在控制器中,我想概述一下这三个表中的实体。我在服务层中的代码如下:

@InjectLogger(<type>)

在前三行中,在三个数据库查询中,所有必需的实体都会从数据库中加载一次。从性能的角度来看,这是可以的。但是:

根据日志,例如在通话时

List<Person> allPersons = personRepository.findAll();
List<Recipient> allRecipients = recipientRepository.findAll();
List<Category> allCategories = categoryRepository.findAll();

for(Person person : allPersons){
    Set<Category> categories = person.getCategories();
    for(Category category : categories){
        // do something with the person and the categories
    }
}

for(Recipient recipient : allRecipients){
    Set<Category> categories = recipient.getCategories();
    for(Category category : categories){
        // do something with the recipient and the categories
    }
}

for(Category category : allCategories){
    Set<Person> persons = category.getPersons();
    for(Person person : persons){
        // do something with the category and the persons
    }
    Set<Recipient> recipients = category.getRecipients();
    for(Recipient recipient : recipients){
        // do something with the category and the recipients
    }
}

在第一个外部for循环中,该服务进行另一个数据库查询,以获取每个人的人的类别,尽管类别已经在前三个代码行中加载。其他循环也是如此。

例如,如果数据库中有5个人,6个收件人和7个类别,则该服务总共执行3 + 5 + 6 + 2 * 7 = 28个数据库查询。显然,这是非常低效的。

我的问题是:

为了使服务仅使用尽可能少的数据库查询就仅获取每个实体一次,我需要更改什么?

1 个答案:

答案 0 :(得分:0)

您面临的问题是典型的休眠N + 1问题。您可以急于获取您的集合并编写自己的查询。

@Query("Select p from Person p join fetch p.categories categories)
public List<Person> fetchAllPerson();

您可以详细了解JPA Hibernate n+1 issue (Lazy & Eager Diff)