One to one hibernate relationship leads to many queries

I have the following class in a one to one relationship

@Entity
@Table(name = "PERSON")
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "PERSON_ID")
    private int personId;
    @Column(name = "PERSON_NAME", nullable = false, length = 30)
    private String personName;
    @OneToOne(mappedBy = "person", cascade = CascadeType.ALL)
    private DrivingLicense drivingLicense;
}

@Entity
@Table(name = "DRIVING_LICENSE")
public class DrivingLicense {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "LICENSE_NUMBER")
    private int licenseNumber;
    @Column(name = "DATE_OF_ISSUE")
    private Date dateOfIssue;
    @OneToOne
    @JoinColumn(name = "PERSON_ID", unique = true)
    private Person person;
}

currently there are 3 rows in each table

but when I make a request personas shown below

Query query = entityManager.createQuery("from Person p");

after receiving the resulting list, which leads to too many queries, as shown below;

Hibernate: select person0_.PERSON_ID as PERSON_ID1_1_, person0_.PERSON_NAME as PERSON_NAME2_1_ from PERSON person0_
Hibernate: select drivinglic0_.LICENSE_NUMBER as LICENSE_NUMBER1_0_1_, drivinglic0_.DATE_OF_ISSUE as DATE_OF_ISSUE2_0_1_, drivinglic0_.PERSON_ID as PERSON_ID3_0_1_, person1_.PERSON_ID as PERSON_ID1_1_0_, person1_.PERSON_NAME as PERSON_NAME2_1_0_ from DRIVING_LICENSE drivinglic0_ left outer join PERSON person1_ on drivinglic0_.PERSON_ID=person1_.PERSON_ID where drivinglic0_.PERSON_ID=?
Hibernate: select drivinglic0_.LICENSE_NUMBER as LICENSE_NUMBER1_0_1_, drivinglic0_.DATE_OF_ISSUE as DATE_OF_ISSUE2_0_1_, drivinglic0_.PERSON_ID as PERSON_ID3_0_1_, person1_.PERSON_ID as PERSON_ID1_1_0_, person1_.PERSON_NAME as PERSON_NAME2_1_0_ from DRIVING_LICENSE drivinglic0_ left outer join PERSON person1_ on drivinglic0_.PERSON_ID=person1_.PERSON_ID where drivinglic0_.PERSON_ID=?
Hibernate: select drivinglic0_.LICENSE_NUMBER as LICENSE_NUMBER1_0_1_, drivinglic0_.DATE_OF_ISSUE as DATE_OF_ISSUE2_0_1_, drivinglic0_.PERSON_ID as PERSON_ID3_0_1_, person1_.PERSON_ID as PERSON_ID1_1_0_, person1_.PERSON_NAME as PERSON_NAME2_1_0_ from DRIVING_LICENSE drivinglic0_ left outer join PERSON person1_ on drivinglic0_.PERSON_ID=person1_.PERSON_ID where drivinglic0_.PERSON_ID=?

it is clear that to retrieve 3 lines sleep mode fired 4 questions, how to solve this problem? Am I something wrong with the relationship?

Update

now if i get a driving license for example

Query query = entityManager.createQuery("from DrivingLicense dl");

its even worse, 7 requests quit.

+4
source share
2 answers

JPA, JPQL.

Person DrivingLicense

Query query = entityManager.createQuery("from Person p join fetch p.drivingLicense");

Person idependent, DrivingLicense

Query query = entityManager.createQuery("from Person p left join fetch p.drivingLicense");

Criteria API

CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Person> criteriaQuery = criteriaBuilder.createQuery(Person.class);
Root<Person> root = criteriaQuery.from(Person.class);
root.fetch("drivingLicense", JoinType.INNER);
criteriaQuery.select(root);
List<Person> resultList = em.createQuery(criteriaQuery).getResultList();

.

CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Person> criteriaQuery = criteriaBuilder.createQuery(Person.class);
Root<Person> root = criteriaQuery.from(Person.class);
root.fetch("drivingLicense", JoinType.LEFT);
criteriaQuery.select(root);
List<Person> resultList = em.createQuery(criteriaQuery).getResultList();
+1

, ..

Query query = entityManager.createQuery("from Person p");

:

session.createCriteria();

1 Person:

Hibernate: this_.id id1_1_1_, this_.name name2_1_1_, rulelic2_.id id1_0_0_, drivinglic2_.DL_no DL_no2_0_0_, drivinglic2_.PERSON_ID PERSON_I3_0_0_ PERSON this_ left external DRIVING_LICENSE drivinglic2_ this_.id = drivinglic2_.PERSON_ID order by this_.id asc

0

Source: https://habr.com/ru/post/1607950/


All Articles