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.
When I run findAll()
a single query is executed for every item in my table.
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:
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". 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.
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:
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. I have tried EntityGraph which changed nothing as well:
@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);
}
CodePudding user response:
You can optimize this with EntityGraph
and lazy loading. I presume VehicleEntity
is the owning entity here. In that case make fetch = FetchType.LAZY
and cascade = CascadeType.ALL
should be on the owning entity. And EntityGraph can be defined 2 ways
- Named Entity Graph
- AD-HOC or Unnamed Entity Graph
Named Entity Graph:
you can define NamedEntityGraph
on owning class and reference it in VehicleEntityRepository
interface
@NamedEntityGraph(name = "VehicleEntity.trackerEntity", attributeNodes = @NamedAttributeNode("trackerEntity"))
public class VehicleEntity {
}
VehicleEntityRepository.java
public interface VehicleEntityRepository extends JpaRepository<VehicleEntity, UUID> {
@EntityGraph(value = "VehicleEntity.trackerEntity", type = EntityGraphType.LOAD)
List<VehicleEntity> findAll();
}
AD-HOC Entity Graph:
You don't have to define any EntityGraph annotation VehicleEntity class but can directly use it Repository interface
VehicleEntityRepository.java
public interface VehicleEntityRepository extends JpaRepository<VehicleEntity, UUID> {
@EntityGraph(attributePaths = { "trackerEntity" })
List<VehicleEntity> findAll();
}
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
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.