I use Hibernate 4.3.8.FINAL and have the following model, in which the Department has many Workers, and the Employee can be a Manager. The manager has a set of Foo, which can be either Foo or Bar.
The essence of the employee:
@Entity
@Table(name = "employee", schema = "payroll")
@Inheritance(strategy = InheritanceType.JOINED)
public class Employee
{
@Id
private Long id;
@Basic(optional = false)
@Column(name = "name")
private String name;
@JoinColumn(name = "department_id", referencedColumnName = "id")
@ManyToOne(optional = false, fetch = FetchType.LAZY)
private Department department;
}
Manager Essence:
@Entity
@Table(name = "manager", schema = "payroll")
@Inheritance(strategy = InheritanceType.JOINED)
@PrimaryKeyJoinColumn(name = "employee_id", referencedColumnName = "id")
public class Manager extends Employee
{
@Basic(optional = false)
@Column(name = "car_allowance")
private boolean carAllowance;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "manager", fetch = FetchType.LAZY)
private Set<Foo> fooSet;
}
Foo Object:
@Entity
@Table(name = "foo", schema = "payroll")
@Inheritance(strategy = InheritanceType.JOINED)
public class Foo
{
@Id
private Long id;
@Basic(optional = false)
@Column(name = "name")
private String name;
@JoinColumn(name = "manager_id", referencedColumnName = "id")
@ManyToOne(fetch = FetchType.LAZY)
private Manager manager;
}
Bar Object:
@Entity
@Table(name = "bar", schema = "payroll")
@Inheritance(strategy = InheritanceType.JOINED)
@PrimaryKeyJoinColumn(name = "foo_id", referencedColumnName = "id")
public class Bar extends Foo
{
@Basic(optional = false)
@Column(name = "name")
private Long size;
}
The essence of the unit:
@NamedEntityGraph(
name = "Graph.Department.Employees",
includeAllAttributes = false,
attributeNodes = {
@NamedAttributeNode(value = "name"),
@NamedAttributeNode(value = "employees", subgraph = "FetchManagers.Subgraph.Employees")
},
subgraphs = {
@NamedSubgraph(
name = "FetchManagers.Subgraph.Employees",
type = Employee.class,
attributeNodes = {
@NamedAttributeNode(value = "name")
}
),
@NamedSubgraph(
name = "FetchManagers.Subgraph.FooBar",
type = Foo.class,
attributeNodes = {
@NamedAttributeNode(value = "name")
}
)
},
subclassSubgraphs = {
@NamedSubgraph(
name = "FetchManagers.Subgraph.Employees",
type = Manager.class,
attributeNodes = {
@NamedAttributeNode(value = "carAllowance"),
@NamedAttributeNode(value = "fooSet", subgraph = "FetchManagers.Subgraph.FooBar")
}
),
@NamedSubgraph(
name = "FetchManagers.Subgraph.FooBar",
type = Bar.class,
attributeNodes = {
@NamedAttributeNode(value = "size")
}
)
}
)
@Entity
@Table(name = "department", schema = "payroll")
public class Department
{
@Id
private Long id;
@Basic(optional = false)
@Column(name = "name")
private String name;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "department", fetch = FetchType.LAZY)
private Set<Employee> employees;
}
As shown in the essence of the Department, I am trying to load fooSet for the Manager using a different subgraph of FetchManagers.Subgraph.FooBar. However, this seems to be completely ignored, and the resulting query is not concatenated with the foo or bar tables (see Query below).
SELECT department0_.id AS id1_153_0_,
employees1_.id AS id1_154_1_,
department0_.name AS name2_153_0_,
employees1_.department_id AS departme3_154_1_,
employees1_.name AS name2_154_1_,
employees1_1_.car_allowance AS car_allo1_156_1_,
CASE
WHEN employees1_1_.employee_id IS NOT NULL THEN 1
WHEN employees1_.id IS NOT NULL THEN 0
END AS clazz_1_,
employees1_.department_id AS departme3_153_0__,
employees1_.id AS id1_154_0__
FROM payroll.department department0_
LEFT OUTER JOIN payroll.employee employees1_ ON department0_.id=employees1_.department_id
LEFT OUTER JOIN payroll.manager employees1_1_ ON employees1_.id=employees1_1_.employee_id
WHERE department0_.id=?
Are nested subgraphs of this type supported? Or am I missing something?