我在一对一的关系中有以下课程
@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;
}
目前每个表中有3行
但当我对person
进行查询时如下
Query query = entityManager.createQuery("from Person p");
获取结果列表后会产生如下所示的太多查询;
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=?
显然对于获取3行hibernate解雇了4个查询,如何解决这个问题?我在关系方面做错了吗?
现在,如果我使用驾驶执照,如
Query query = entityManager.createQuery("from DrivingLicense dl");
更糟糕的是,7个查询被解雇。
答案 0 :(得分:1)
要拥有独立于JPA供应商的解决方案,您可以使用以下JPQL查询。
使用Person
DrivingLicense
Query query = entityManager.createQuery("from Person p join fetch p.drivingLicense");
如果他们有Person
或不是,则让所有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();
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();
答案 1 :(得分:0)
这似乎是由于您的疑问,即
Query query = entityManager.createQuery("from Person p");
改为使用:
session.createCriteria();
这将只触发1个查询来获取Person:
休眠:选择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_ left outer 在this_.id = drivinglic2_.PERSON_ID上加入DRIVING_LICENSE drivinglic2_ 由this_.id asc
命令