【发布时间】:2017-07-19 09:07:23
【问题描述】:
我有那些实体,它不是来自真实代码,但我认为这并不重要。
@Entity
public class Country{
private String name;
//other fields with getters and setters
@OneToMany
private List<City> cites;
}
@Entity
public class City {
private String name;
//other fields with getters and setters
@OneToMany
private List<Employee> emps;//list size could be millions
}
@Entity
public class Employee {
private String name;
//other fields with getters and setters
@OneToMany
private List<Cars> cars;// list size could be 1000
}
@Entity
public class Car {
private String name;
@ManyToOne
private Employee emp;
}
我想获得单个 Car 实体,但也需要像这样的其他数据(国家、城市、员工)
1 个国家,1 个城市,1 个员工,1 个汽车(我选择了哪个 ID)
所以当我从国家加入 jpa 时,就像
select c from Country c
inner join c.cites ci
inner join ci.emps em
inner join ci.cars car where car.id = ?
我获得了 Country(包括所有城市)的所有数据。
我应该怎么做才能得到 1 Country,1 City, 1 Employee, 1 Car.
如果用一个 jpa 查询无法做到这一点,请建议其他方式。
所有的关系都是双向的和惰性的。
I tried this way.
1)select car by id. then get Id of employee from car.
2)select Employee by id - but at this point Employee data are nulls -
我认为这是因为从 Car 到 Employee 的 ManyToOne 是懒惰的。你怎么看?
【问题讨论】:
标签: java hibernate jpa jpql querydsl