[英]One to one relationship hibernate results in many queries
I have following class in 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 目前每个表中有3行
but when I do a query on person
like below 但是当我对下面的person
进行查询时
Query query = entityManager.createQuery("from Person p");
after getting resultlist its resulting in too many queries like 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=?
clearly for fetching 3 rows hibernate fired 4 queries, how to solve this problem? 显然对于获取3行hibernate的4个查询,如何解决这个问题? am I doing something wrong with relationships? 我在关系方面做错了吗?
now if I fetch with Driving license like 现在如果我拿着驾驶执照就像
Query query = entityManager.createQuery("from DrivingLicense dl");
its even more worse, 7 queries are fired. 更糟糕的是,7个查询被解雇了。
To have a JPA vendor independent solution you could use following JPQL queries. 要拥有JPA供应商独立解决方案,您可以使用以下JPQL查询。
get all Person
with a DrivingLicense
让所有Person
有DrivingLicense
Query query = entityManager.createQuery("from Person p join fetch p.drivingLicense");
get all Person
idependent if they have a DrivingLicense
or not 让所有Person
idependent如果他们有DrivingLicense
或不
Query query = entityManager.createQuery("from Person p left join fetch p.drivingLicense");
or using the Criteria API
或使用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();
resp. RESP。
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();
This seems due to your query, ie 这似乎是由于您的查询,即
Query query = entityManager.createQuery("from Person p");
Instead use: 而是使用:
session.createCriteria();
This will fire only 1 query to fetch Person: 这将只触发1个查询以获取Person:
Hibernate: select this_.id as id1_1_1_, this_.name as name2_1_1_, drivinglic2_.id as id1_0_0_, drivinglic2_.DL_no as DL_no2_0_0_, drivinglic2_.PERSON_ID as PERSON_I3_0_0_ from PERSON this_ left outer join DRIVING_LICENSE drivinglic2_ on this_.id=drivinglic2_.PERSON_ID order by this_.id asc Hibernate:选择this_.id为id1_1_1_,this_.name为name2_1_1_,drivinglic2_.id为id1_0_0_,drivinglic2_.DL_no为DL_no2_0_0_,drivinglic2_.PERSON_ID为PERSON_I3_0_0_来自PERSON this_左外连接DRIVING_LICENSE drivinglic2_ on this_.id = drivinglic2_.PERSON_ID order by this_.id asc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.