I'm using Hibernate 4.3.8.FINAL and have the following model where a Department has many Employees, and an Employee can be a Manager. Manager has a set of Foo which can be either Foo or Bar.
The Employee entity:
@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;
}
The Manager entity:
@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;
}
The Foo entity:
@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;
}
The Bar entity:
@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 Department entity:
@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 Department entity, I'm trying to load the fooSet for Manager using another subgraph FetchManagers.Subgraph.FooBar. However, this seems to be totally ignored, and the resultant query does not join 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?