【问题标题】:JPA 2.1 NamedSubgraph in Hibernate ignoring subclassesHibernate中的JPA 2.1 NamedSubgraph忽略子类
【发布时间】:2015-06-18 16:00:52
【问题描述】:

我正在使用 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 属性,或者我错过了什么?

【问题讨论】:

    标签: java spring hibernate jpa jpa-2.1


    【解决方案1】:

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

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

    @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"),
                }
            )
        }
    )
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    • 2010-10-07
    • 2016-08-31
    • 1970-01-01
    • 1970-01-01
    • 2011-06-05
    相关资源
    最近更新 更多