我正在使用Hibernate 4.3.8.FINAL并且具有以下模型,其中Department有许多Employees,而Employee可以是Manager。经理有一套Foo,可以是Foo或Bar。
员工实体:
@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;
}
经理实体:
@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实体:
@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实体:
@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;
}
部门实体:
@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;
}
如部门实体所示,我正在尝试使用另一个子图FetchManagers.Subgraph.FooBar为Manager加载fooSet。但是,这似乎完全被忽略了,结果查询不会与foo或bar表连接(参见下面的查询)。
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=?
是否支持此类型的嵌套子图?或者我错过了什么?