Hibernate 中的 JPA 2.1 NamedSubgraph 忽略子类

JPA 2.1 NamedSubgraph in Hibernate ignoring subclasses

我使用的是 Hibernate 4.3。8.FINAL 并且有以下模型,其中一个部门有很多员工,一个员工可以是一个经理。

员工实体:

@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;
}

部门实体:

@NamedEntityGraph(
        name = "Graph.Department.FetchManagers",
        includeAllAttributes = false,
        attributeNodes = {
                @NamedAttributeNode(value = "name"),
                @NamedAttributeNode(value = "employees", subgraph = "FetchManagers.Subgraph.Managers")
        },
        subgraphs = {
                @NamedSubgraph(
                        name = "FetchManagers.Subgraph.Managers",
                        type = Employee.class,
                        attributeNodes = {
                                @NamedAttributeNode(value = "name")
                        }
                ),
                @NamedSubgraph(
                        name = "FetchManagers.Subgraph.Managers",
                        type = Manager.class,
                        attributeNodes = {
                                @NamedAttributeNode(value = "carAllowance"),
                        }
                )
        }
)
@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;
}

如 Department 实体所示,我正在尝试创建一个 @NamedSubgraph 来加载所有员工并获取 Manager.carAllowance。但是我收到以下错误:

Unable to locate Attribute  with the the given name [carAllowance] on this ManagedType [com.nemea.hydra.model.test.Employee]

根据我的理解,@NamedSubgraph.type 应该用于指定要获取的实体子类属性。 Hibernate 是否有可能忽略 @NamedSubgraph 注释的 type=Manager.class 属性,或者我是否遗漏了什么?

这可能是 Hibernate 4.3 的缺陷。8.FINAL,例如使用 subgraphs 属性时,EclipseLink 2.5.1 不会抛出异常。

无论如何,在 Manager 类型的情况下,当您指定 subclassSubgraphs 而不是 subclass 时它应该可以工作,即:

@NamedEntityGraph(
    name = "Graph.Department.FetchManagers", 
    includeAllAttributes = false,
    attributeNodes = {
        @NamedAttributeNode(value = "name"),
        @NamedAttributeNode(value = "employees", subgraph = "FetchManagers.Subgraph.Managers")
    },
    subgraphs = {
        @NamedSubgraph(
            name = "FetchManagers.Subgraph.Managers",
            type = Employee.class,
            attributeNodes = {
                @NamedAttributeNode(value = "name")
            }
        )
    },
    subclassSubgraphs = {
        @NamedSubgraph(
            name = "FetchManagers.Subgraph.Managers",
            type = Manager.class,
            attributeNodes = {
                @NamedAttributeNode(value = "carAllowance"),
            }
        )
    }
)