简体   繁体   English

如何在避免 n+1 查询的同时设置 JPA 双向一对一

[英]How to setup JPA bi-directional one to one while avoiding n+1 queries

I'm trying to debug two entities that have a bi-directional one to one relationship.我正在尝试调试具有双向一对一关系的两个实体。 The problem is that the relationship is causing N+1 Queries to occur.问题在于该关系导致发生 N+1 查询。 When I run findAll() a single query is executed for every item in my table.当我运行findAll()时,会针对我表中的每个项目执行单个查询。

Here is an example: I have two entities这是一个例子:我有两个实体

package ...;
import ...;

@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor
@EqualsAndHashCode
@Getter
@Builder

@Entity
@Table(name = "tracker")
public class TrackerEntity {
    @Id
    @Column(name = "vehicleid")
    private UUID vehicleId;

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

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "vehicleid", referencedColumnName = "id")
    private VehicleEntity vehicleEntity;
}

@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor
@EqualsAndHashCode
@Getter
@Builder

@Entity
@Table(name = "vehicle")
public class VehicleEntity {
    @Id
    private UUID id;

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

    @OneToOne(mappedBy = "vehicleEntity", cascade = CascadeType.REMOVE, orphanRemoval = true)
    private TrackerEntity trackerEntity;
}

When I run vehicleRepository.findAll() I get following debug logs:当我运行vehicleRepository.findAll()时,我得到以下调试日志:

2022-05-17 19:16:45.071 DEBUG 62639 --- [           main] org.hibernate.SQL                        : select vehicleent0_.id as id1_1_0_, vehicleent0_.vehiclename as vehiclen2_1_0_ from vehicle vehicleent0_ where vehicleent0_.id=?
2022-05-17 19:16:45.097 DEBUG 62639 --- [           main] org.hibernate.SQL                        : insert into vehicle (vehiclename, id) values (?, ?)
2022-05-17 19:16:45.104 DEBUG 62639 --- [           main] org.hibernate.SQL                        : select vehicleent0_.id as id1_1_0_, vehicleent0_.vehiclename as vehiclen2_1_0_ from vehicle vehicleent0_ where vehicleent0_.id=?
2022-05-17 19:16:45.106 DEBUG 62639 --- [           main] org.hibernate.SQL                        : insert into vehicle (vehiclename, id) values (?, ?)
2022-05-17 19:16:45.109 DEBUG 62639 --- [           main] org.hibernate.SQL                        : select vehicleent0_.id as id1_1_0_, vehicleent0_.vehiclename as vehiclen2_1_0_ from vehicle vehicleent0_ where vehicleent0_.id=?
2022-05-17 19:16:45.110 DEBUG 62639 --- [           main] org.hibernate.SQL                        : insert into vehicle (vehiclename, id) values (?, ?)
2022-05-17 19:16:45.113 DEBUG 62639 --- [           main] org.hibernate.SQL                        : select vehicleent0_.id as id1_1_0_, vehicleent0_.vehiclename as vehiclen2_1_0_ from vehicle vehicleent0_ where vehicleent0_.id=?
2022-05-17 19:16:45.115 DEBUG 62639 --- [           main] org.hibernate.SQL                        : insert into vehicle (vehiclename, id) values (?, ?)
2022-05-17 19:16:45.117 DEBUG 62639 --- [           main] org.hibernate.SQL                        : select vehicleent0_.id as id1_1_0_, vehicleent0_.vehiclename as vehiclen2_1_0_ from vehicle vehicleent0_ where vehicleent0_.id=?
2022-05-17 19:16:45.119 DEBUG 62639 --- [           main] org.hibernate.SQL                        : insert into vehicle (vehiclename, id) values (?, ?)
2022-05-17 19:16:45.123 DEBUG 62639 --- [           main] org.hibernate.SQL                        : select vehicleent0_.id as id1_1_0_, vehicleent0_.vehiclename as vehiclen2_1_0_ from vehicle vehicleent0_ where vehicleent0_.id=?
2022-05-17 19:16:45.125 DEBUG 62639 --- [           main] org.hibernate.SQL                        : insert into vehicle (vehiclename, id) values (?, ?)
2022-05-17 19:16:45.128 DEBUG 62639 --- [           main] org.hibernate.SQL                        : select vehicleent0_.id as id1_1_0_, vehicleent0_.vehiclename as vehiclen2_1_0_ from vehicle vehicleent0_ where vehicleent0_.id=?
2022-05-17 19:16:45.130 DEBUG 62639 --- [           main] org.hibernate.SQL                        : insert into vehicle (vehiclename, id) values (?, ?)
2022-05-17 19:16:45.133 DEBUG 62639 --- [           main] org.hibernate.SQL                        : select vehicleent0_.id as id1_1_0_, vehicleent0_.vehiclename as vehiclen2_1_0_ from vehicle vehicleent0_ where vehicleent0_.id=?
2022-05-17 19:16:45.135 DEBUG 62639 --- [           main] org.hibernate.SQL                        : insert into vehicle (vehiclename, id) values (?, ?)
2022-05-17 19:16:45.138 DEBUG 62639 --- [           main] org.hibernate.SQL                        : select vehicleent0_.id as id1_1_0_, vehicleent0_.vehiclename as vehiclen2_1_0_ from vehicle vehicleent0_ where vehicleent0_.id=?
2022-05-17 19:16:45.141 DEBUG 62639 --- [           main] org.hibernate.SQL                        : insert into vehicle (vehiclename, id) values (?, ?)
2022-05-17 19:16:45.144 DEBUG 62639 --- [           main] org.hibernate.SQL                        : select vehicleent0_.id as id1_1_0_, vehicleent0_.vehiclename as vehiclen2_1_0_ from vehicle vehicleent0_ where vehicleent0_.id=?
2022-05-17 19:16:45.146 DEBUG 62639 --- [           main] org.hibernate.SQL                        : insert into vehicle (vehiclename, id) values (?, ?)
2022-05-17 19:16:45.246 DEBUG 62639 --- [           main] org.hibernate.SQL                        : select vehicleent0_.id as id1_1_, vehicleent0_.vehiclename as vehiclen2_1_ from vehicle vehicleent0_
2022-05-17 19:16:45.253 DEBUG 62639 --- [           main] org.hibernate.SQL                        : select trackerent0_.vehicleid as vehiclei1_0_0_, trackerent0_.trackerid as trackeri2_0_0_ from tracker trackerent0_ where trackerent0_.vehicleid=?
2022-05-17 19:16:45.258 DEBUG 62639 --- [           main] org.hibernate.SQL                        : select trackerent0_.vehicleid as vehiclei1_0_0_, trackerent0_.trackerid as trackeri2_0_0_ from tracker trackerent0_ where trackerent0_.vehicleid=?
2022-05-17 19:16:45.259 DEBUG 62639 --- [           main] org.hibernate.SQL                        : select trackerent0_.vehicleid as vehiclei1_0_0_, trackerent0_.trackerid as trackeri2_0_0_ from tracker trackerent0_ where trackerent0_.vehicleid=?
2022-05-17 19:16:45.261 DEBUG 62639 --- [           main] org.hibernate.SQL                        : select trackerent0_.vehicleid as vehiclei1_0_0_, trackerent0_.trackerid as trackeri2_0_0_ from tracker trackerent0_ where trackerent0_.vehicleid=?
2022-05-17 19:16:45.262 DEBUG 62639 --- [           main] org.hibernate.SQL                        : select trackerent0_.vehicleid as vehiclei1_0_0_, trackerent0_.trackerid as trackeri2_0_0_ from tracker trackerent0_ where trackerent0_.vehicleid=?
2022-05-17 19:16:45.264 DEBUG 62639 --- [           main] org.hibernate.SQL                        : select trackerent0_.vehicleid as vehiclei1_0_0_, trackerent0_.trackerid as trackeri2_0_0_ from tracker trackerent0_ where trackerent0_.vehicleid=?
2022-05-17 19:16:45.265 DEBUG 62639 --- [           main] org.hibernate.SQL                        : select trackerent0_.vehicleid as vehiclei1_0_0_, trackerent0_.trackerid as trackeri2_0_0_ from tracker trackerent0_ where trackerent0_.vehicleid=?
2022-05-17 19:16:45.267 DEBUG 62639 --- [           main] org.hibernate.SQL                        : select trackerent0_.vehicleid as vehiclei1_0_0_, trackerent0_.trackerid as trackeri2_0_0_ from tracker trackerent0_ where trackerent0_.vehicleid=?
2022-05-17 19:16:45.269 DEBUG 62639 --- [           main] org.hibernate.SQL                        : select trackerent0_.vehicleid as vehiclei1_0_0_, trackerent0_.trackerid as trackeri2_0_0_ from tracker trackerent0_ where trackerent0_.vehicleid=?
2022-05-17 19:16:45.271 DEBUG 62639 --- [           main] org.hibernate.SQL                        : select trackerent0_.vehicleid as vehiclei1_0_0_, trackerent0_.trackerid as trackeri2_0_0_ from tracker trackerent0_ where trackerent0_.vehicleid=?

I've searched for N+1 JPA Query problems in hopes of finding a solution and most either suggested using EntityGraph or "bytecode enhancement".我搜索了 N+1 JPA 查询问题,希望找到解决方案,大多数建议使用 EntityGraph 或“字节码增强”。 I've tried using entitygraph and still got n+1 queries and I don't want to use bytecode enhancement since the application has to run on a cloud as a microservice.我已经尝试使用实体图,但仍然有 n+1 个查询,我不想使用字节码增强,因为应用程序必须作为微服务在云上运行。

One more important thing is I want Trackers to be removed when their Vehicle is removed but they should not be created when a vehicle is created.更重要的一件事是我希望在移除车辆时删除跟踪器,但不应在创建车辆时创建跟踪器。

Is there any way to fix this?有没有什么办法解决这一问题?

I have tried using Query and join fetch:我试过使用 Query 和 join fetch:

public interface VehicleRepository extends JpaRepository<VehicleEntity, UUID> {

    @Override
    @Query("SELECT v FROM VehicleEntity v left outer join fetch v.trackerEntity t")
    <S extends VehicleEntity> List<S> findAll(Example<S> example);
}

which did not change anything, still got n+1 queries.这并没有改变任何东西,仍然有 n+1 个查询。 I have tried EntityGraph which changed nothing as well:我试过 EntityGraph 也没有改变任何东西:

@Entity
@Table(name = "vehicle")
@NamedEntityGraph(name="vehicleGraph", attributeNodes = {@NamedAttributeNode("trackerEntity")})
public class VehicleEntity {
    @Id
    private UUID id;

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

    @OneToOne(mappedBy = "vehicleEntity", cascade = CascadeType.REMOVE, orphanRemoval = true)
    private TrackerEntity trackerEntity;
}
public interface VehicleRepository extends JpaRepository<VehicleEntity, UUID> {

    @Override
    @EntityGraph(value = "vehicleGraph", type = EntityGraph.EntityGraphType.LOAD)
    <S extends VehicleEntity> List<S> findAll(Example<S> example);
}

You can optimize this with EntityGraph and lazy loading.您可以使用EntityGraph和延迟加载对其进行优化。 I presume VehicleEntity is the owning entity here.我假设VehicleEntity是这里的拥有实体。 In that case make fetch = FetchType.LAZY and cascade = CascadeType.ALL should be on the owning entity.在那种情况下, make fetch = FetchType.LAZYcascade = CascadeType.ALL应该在拥有的实体上。 And EntityGraph can be defined 2 ways EntityGraph 可以通过两种方式定义

  1. Named Entity Graph命名实体图
  2. AD-HOC or Unnamed Entity Graph AD-HOC 或未命名实体图

Named Entity Graph:命名实体图:

you can define NamedEntityGraph on owning class and reference it in VehicleEntityRepository interface您可以在拥有 class 时定义NamedEntityGraph并在VehicleEntityRepository接口中引用它

@NamedEntityGraph(name = "VehicleEntity.trackerEntity", attributeNodes = @NamedAttributeNode("trackerEntity"))
public class VehicleEntity {
}

VehicleEntityRepository.java VehicleEntityRepository.java

public interface VehicleEntityRepository extends JpaRepository<VehicleEntity, UUID> {
     @EntityGraph(value = "VehicleEntity.trackerEntity", type = EntityGraphType.LOAD)
    List<VehicleEntity> findAll();
}

AD-HOC Entity Graph: AD-HOC 实体图:

You don't have to define any EntityGraph annotation VehicleEntity class but can directly use it Repository interface您不必定义任何 EntityGraph 注释 VehicleEntity class 而是可以直接使用它 Repository 接口

VehicleEntityRepository.java VehicleEntityRepository.java

public interface VehicleEntityRepository extends JpaRepository<VehicleEntity, UUID> {
    @EntityGraph(attributePaths = { "trackerEntity" })
    List<VehicleEntity> findAll();
}

VehicleEntity.java VehicleEntity.java

@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor
@EqualsAndHashCode
@Getter
@Builder

@Entity
@Table(name = "vehicle")
@NamedEntityGraph(name = "VehicleEntity.trackerEntity", attributeNodes = @NamedAttributeNode("trackerEntity"))
public class VehicleEntity {
    @Id
    private UUID id;

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

 
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "vehicleEntity")
    @JsonManagedReference
    private TrackerEntity trackerEntity;
}

TrackerEntity.java TrackerEntity.java

package ...;
import ...;

@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor
@EqualsAndHashCode
@Getter
@Builder

@Entity
@Table(name = "tracker")
public class TrackerEntity {
    @Id
    @Column(name = "id")
    private UUID id;

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

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "vehicleid", referencedColumnName = "id")
@JsonBackReference
    private VehicleEntity vehicleEntity;
}
    

You can also use @JsonManagedReference and @JsonBackReference to prevent nested fetch or infinite recursion when using Jackson.在使用 Jackson 时,您还可以使用@JsonManagedReference@JsonBackReference来防止嵌套获取或无限递归。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM