N+1 Hibernate.

I've got problem with fetching data. Here is my code: @NoArgsConstructor @AllArgsConstructor @Getter @Setter @Entity @SuperBuilder @DiscriminatorColumn(name = "dtype") public abstract class Person{ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String name; private String surname; } @Entity @Getter @Setter @NoArgsConstructor @AllArgsConstructor @SuperBuilder public class Employee extends Person { @OneToMany(mappedBy = "employee") private List<Position> positions = new ArrayList<>(); } @NoArgsConstructor @AllArgsConstructor @Getter @Setter @Entity @SuperBuilder @ToString public class Position { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "employee_id") private Employee employee; private String position; private LocalDate startDate; private LocalDate endDate; } @Transactional(readOnly = true) public Page<PersonDto> findAll(Pageable pageable, Map<String, String> params) { Specification<Person> filter = FilterSpecification.byCriteria(params); return personRepository.findAll(filter, pageable) .map(person -> creationStrategies.get(person.getType()).toDto(person)); } I've got one superclass like Person becauseI can create a lot of person typelike Employee, alien, kid, retiree with deflaut params. But when I'm fetching all the people using findAll from JPA I'v got this problem: Hibernate: select distinct p10.id,p1_0.dtype,p1_0.email,p1_0.height,p1_0.name,p1_0.person_number,p1_0.surname,p1_0.type,p1_0.version,p1_0.weight,p1_0.pension_amount,p1,p1_0.university_name,p1_0.year_of_study from person p1_0 where 1=1 offset ? rows fetch first ? rows only Hibernate: select p1_0.employee_id,p1_0.id,p1_0.end_date,p1_0.position,p1_0.salary,p1_0.start_date from position p1_0 where p1_0.employee_id=? I'm tried fetchType.Eager, Batch and all of them doesn't work.
3 Replies
JavaBot
JavaBot3w ago
This post has been reserved for your question.
Hey @F4F! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
JavaBot
JavaBot3w ago
Please format your code to make it more readable. For java, it should look like this:
​`​`​`​java
public void foo() {

}
​`​`​`​
​`​`​`​java
public void foo() {

}
​`​`​`​
JavaBot
JavaBot3w ago
Post Closed
This post has been closed by <@510548208360095745>.
Want results from more Discord servers?
Add your server