'subclassSubgraphs of @NamedEntityGraph does not work
Hello I saw some similar questions from 2018 but without much information
I have the following classes
- Employee
@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;
}
- Address
@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;
}
- City
@Entity
@Setter
@Getter
public class City {
@Id
String id;
String name;
}
- Engineer
@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;
}
- Laptop
@Entity
@Getter
@Setter
public class Laptop {
@Id
@Column(name = "employee_id")
String employeeId;
String brand;
}
With the NamedEntityGraph I want to achieve in a single query to retrieve all the information of the employee. The attributeNodes and the subgraph for city are working correctly but the subclassSubgraphs is not working for this example..
my expectation was that I will retrieve the laptop information in one query with a join clause but actually there are two select statements
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=?
I cannot find any page that reports a bug about this, am I doing something wrong?
code repo -> https://github.com/dkasiaras/pocs/tree/main/jpa-graph-entity
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
