您好,我从 2018 年看到了一些类似的问题,但没有太多信息
我有以下课程
- 员工
@Entity
@Getter
@Setter
@NoArgsConstructor
@NamedEntityGraph(name = "employee.complete", attributeNodes = {
@NamedAttributeNode(
value = "addresses",
subgraph = "address_city"
)},
subgraphs = {
@NamedSubgraph(
name = "address_city",
attributeNodes = {
@NamedAttributeNode("city")
})
},
subclassSubgraphs = {
@NamedSubgraph(
name = "noUse",
type = Engineer.class,
attributeNodes = {
@NamedAttributeNode("laptop")
})
}
)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "profession")
public class Employee {
@Id
String id;
String name;
String email;
@OneToMany
@JoinColumn(name = "employee_id", updatable = false)
List<Address> addresses;
}
- 地址
@Entity
@Getter
@Setter
@ToString
@NoArgsConstructor
public class Address {
@Id
private String addressId;
private String streetName;
private String streetNumber;
@OneToOne
@JoinColumn(name = "city_id")
private City city;
}
- 城市
@Entity
@Setter
@Getter
public class City {
@Id
String id;
String name;
}
- 工程师
@Entity
@Getter
@Setter
@DiscriminatorValue("engineer")
public class Engineer extends Employee {
private String seniority;
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "id")
Laptop laptop;
}
- 笔记本电脑
@Entity
@Getter
@Setter
public class Laptop {
@Id
@Column(name = "employee_id")
String employeeId;
String brand;
}
使用 NamedEntityGraph,我想在单个查询中实现检索员工的所有信息。城市的属性节点和子图工作正常,但子类子图不适用于此示例。
我的期望是我将在一个带有 join 子句的查询中检索笔记本电脑信息,但实际上有两个 select 语句
select employee0_.id as id2_3_0_, addresses1_.address_id as address_2_0_1_, city2_.id as id1_1_2_, employee0_.email as email3_3_0_, employee0_.name as name4_3_0_, employee0_.seniority as seniorit5_3_0_, employee0_.profession as professi1_3_0_, addresses1_.city_id as city_id5_0_1_, addresses1_.street_name as street_n3_0_1_, addresses1_.street_number as street_n4_0_1_, addresses1_.address_type as address_1_0_1_, addresses1_.employee_id as employee6_0_0__, addresses1_.address_id as address_2_0_0__, city2_.name as name2_1_2_ from employee employee0_ left outer join address addresses1_ on employee0_.id=addresses1_.employee_id left outer join city city2_ on addresses1_.city_id=city2_.id where employee0_.id=? and (employee0_.id is not null)
select laptop0_.employee_id as employee1_4_0_, laptop0_.brand as brand2_4_0_ from laptop laptop0_ where laptop0_.employee_id=?
我找不到任何报告有关此错误的页面,我做错了什么吗?
代码仓库-> https://github.com/dkasiaras/pocs/tree/main/jpa-graph-entity