我正在分析Spring Data为我的模型生成的查询:
public class Description {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Short id;
@Version
Timestamp version;
@OneToOne(mappedBy="description")
@Getter @Setter
Customer customer;
@Getter @Setter
String description;
}
public class WorkPlace {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Short id;
@Version
Timestamp version;
@Getter @Setter
String workplaceName;
@OneToMany(mappedBy="workPlace")
Set<Customer> customers;
}
可能不重要的部分:
public class Address {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Short id;
@Version
Timestamp version;
@ManyToMany @Getter
List<Customer> customers = new LinkedList<>();
@Getter @Setter
String street;
}
public class CustomerOrder {
@Id
@GeneratedValue
private Short id;
@Version
Timestamp version;
@ManyToOne @Getter @Setter
Customer customer;
@Getter @Setter
int quantity;
}
我有一个适用于客户的Spring Data CRUD存储库。 调用后:repo.findOne((简称)1)生成以下查询:
select customer0_.id as id11_2_, customer0_.description_id as descript4_11_2_, customer0_.name as name11_2_, customer0_.version as version11_2_, customer0_.workPlace_id as workPlace5_11_2_, descriptio1_.id as id12_0_, descriptio1_.description as descript2_12_0_, descriptio1_.version as version12_0_, workplace2_.id as id15_1_, workplace2_.version as version15_1_, workplace2_.workplaceName as workplac3_15_1_
from Customer customer0_ left outer join
Description descriptio1_ on customer0_.description_id=descriptio1_.id left outer join
WorkPlace workplace2_ on customer0_.workPlace_id=workplace2_.id
where customer0_.id=?
select customer0_.id as id11_2_, customer0_.description_id as descript4_11_2_, customer0_.name as name11_2_, customer0_.version as version11_2_, customer0_.workPlace_id as workPlace5_11_2_, descriptio1_.id as id12_0_, descriptio1_.description as descript2_12_0_, descriptio1_.version as version12_0_, workplace2_.id as id15_1_, workplace2_.version as version15_1_, workplace2_.workplaceName as workplac3_15_1_
from Customer customer0_ left outer join
Description descriptio1_ on customer0_.description_id=descriptio1_.id left outer join
WorkPlace workplace2_ on customer0_.workPlace_id=workplace2_.id
where customer0_.description_id=?
为什么我们需要第二个查询?第一个还不够吗? 我错过了什么,或者同一个查询(相同的结果)被调用了两次?