春季如何防止执行第二个查询?

时间:2020-06-19 18:09:56

标签: java spring-boot hibernate spring-data-jpa

从数据库中获取某些实体时,JPA / Hibernate存在问题。问题是它执行两个查询,即使第一个查询也足以收集我需要的实体数据。

我要联系的实体类型具有自我参考。在这里:

@Data
@Entity
@Table(name = "franchises")
@EntityListeners(AuditingEntityListener.class)
@NamedEntityGraphs({
    @NamedEntityGraph(
        name = "Franchise.Parent.Country",
        attributeNodes = {
            @NamedAttributeNode("parent"),
            @NamedAttributeNode("country"),
        }
    ),
})
public class Franchise {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(name = "name")
    private String name;

    @Enumerated(EnumType.STRING)
    @Column(name = "type")
    private FranchiseType type;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_id")
    @EqualsAndHashCode.Exclude
    @ToString.Exclude
    private Franchise parent;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "country_id")
    @EqualsAndHashCode.Exclude
    @ToString.Exclude
    private Country country;
}

这是存储库:

@Repository
public interface IFranchiseRepository extends JpaRepository<Franchise, Long>,
    JpaSpecificationExecutor<Franchise>,
    QuerydslPredicateExecutor<Franchise>,
    FranchiseRepositoryCustom
{
    @EntityGraph("Franchise.Parent.Country")
    Optional<Franchise> findWithParentAndCountryById(long id);
}

现在在控制器中,我有以下代码:

@GetMapping("/api/franchises/{franchise}")
public FranchiseDTO getFranchise(@PathVariable("franchise") long franchiseId) {
     var f = franchiseRepository.findWithParentAndCountryById(franchiseId).get();
     return null; // Do not worry about this statement, I am yet to map ti to DTO
}

当我调用此api端点时,我可以在控制台日志中看到它正在执行两个查询:

SELECT
    franchise0_.id AS id1_6_0_,
    franchise1_.id AS id1_6_1_,
    country2_.id AS id1_3_2_,
    franchise0_.country_id AS country_6_6_0_,
    franchise0_.created_at AS created_2_6_0_,
    franchise0_.name AS name3_6_0_,
    franchise0_.parent_id AS parent_i7_6_0_,
    franchise0_.type AS type4_6_0_,
    franchise0_.updated_at AS updated_5_6_0_,
    franchise1_.country_id AS country_6_6_1_,
    franchise1_.created_at AS created_2_6_1_,
    franchise1_.name AS name3_6_1_,
    franchise1_.parent_id AS parent_i7_6_1_,
    franchise1_.type AS type4_6_1_,
    franchise1_.updated_at AS updated_5_6_1_,
    country2_.alpha2_code AS alpha2_3_2_,
    country2_.alpha3_code AS alpha3_3_2_,
    country2_.created_at AS created_4_3_2_,
    country2_.name AS name5_3_2_,
    country2_.updated_at AS updated_6_3_2_
FROM
    franchises franchise0_
LEFT OUTER JOIN franchises franchise1_ ON
    franchise0_.parent_id = franchise1_.id
LEFT OUTER JOIN countries country2_ ON
    franchise0_.country_id = country2_.id
WHERE
    franchise0_.id = 3;

SELECT
    franchise0_.id AS id1_6_0_,
    franchise0_.country_id AS country_6_6_0_,
    franchise0_.created_at AS created_2_6_0_,
    franchise0_.name AS name3_6_0_,
    franchise0_.parent_id AS parent_i7_6_0_,
    franchise0_.type AS type4_6_0_,
    franchise0_.updated_at AS updated_5_6_0_
FROM
    franchises franchise0_
WHERE
    franchise0_.id = 135

这是我在数据库中拥有的数据:

|-----|-------------|---------------|-----------|------------|  
|  id | name        | type          | parent_id | country_id |  
|-----|-------------|---------------|-----------|------------|  
| 135 | Franchise A | INTERNATIONAL | NULL      | NULL       |  
|  2  | Franchise B | MASTER        | 135       |  1         |  
|  3  | Franchise C | REGIONAL      |  2        |  1         |  
|  4  | Franchise D | REGIONAL      |  2        |  1         |  
|-----|-------------|---------------|-----------|------------|  

因此,通过查看已执行的查询,我可以看到(并测试)使用第一个查询Franchise + Parent + Country来获取我所需的所有数据。但是然后执行了第二个查询以加载我不需要的父级...

有人可以告诉我我在做什么错吗?为什么执行两个查询而不是仅执行一个查询?

1 个答案:

答案 0 :(得分:0)

更改此:

@ManyToOne(fetch = FetchType.LAZY)
private Franchise parent;

@ManyToOne(fetch = FetchType.EAGER)
private Franchise parent;

然后尝试。

相关问题