gpt4 book ai didi

java - Hibernate 中的 JPA 2.1 NamedSubgraph 忽略子类

转载 作者:搜寻专家 更新时间:2023-11-01 02:24:14 36 4
gpt4 key购买 nike

我正在使用 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"),
}
)
}
)

关于java - Hibernate 中的 JPA 2.1 NamedSubgraph 忽略子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29603695/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com