我有一点JPA问题。 假设你有这样的表:(结构是固定的)
PERSON
--
ID,
DEPARTMENT_ID
...
SPECIALWORKER
------
ID, PERSON_ID, SPECIALDEPARTMENT_ID
...
DEPARTMENT
-------
ID,
...
SPECIALDEPARTMENT
-------
ID,
...
在Java中,我将使用简单的层次结构构建它:SpecialWorker extends Person
和SpecialDepartment extends Department
。我们将拥有简单的"人和简单"部门。
在JPA中,我尝试使用JOINED_Table继承构建该场景,但我无法使其正常工作。有什么想法吗?
修改的
编码,我希望我什么都不会错过。 当我尝试插入一个特殊工作者时,我得到了完整性异常。 当我插入一个特殊工作者时,jpa必须将fk设置为离开(基类人)以及将fk设置为特殊部分(来自具体当前类)
@Entity
@Table(name = "DEPARTEMENT")
@Access(AccessType.FIELD)
@Inheritance(strategy = InheritanceType.JOINED)
public class Departement
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private Integer id;
...
}
@Entity
@Table(name = "SPECIALDEPARTEMENT")
@Access(AccessType.FIELD)
@PrimaryKeyJoinColumn(name = "ID_DEPARTEMENT", referencedColumnName = "ID")
public class SpecialDepartement
extends Departement
implements Serializable
{
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID", updatable = false, insertable = false)
private Integer id01;
@OneToMany(mappedBy = "specialDepartement", cascade = CascadeType.ALL, orphanRemoval = true)
private List<SpecialWorker> workers;
...
}
@Entity
@Table(name = "PERSON")
@Access(AccessType.FIELD)
@Inheritance(strategy = InheritanceType.JOINED)
public class Person
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
@Max(19)
private Long id;
@ManyToOne
@JoinColumn(name = "ID_DEPARTEMENT", referencedColumnName = "ID", nullable = false)
private Departement departement;
...
}
@Entity
@Table(name = "SWORKER")
@Access(AccessType.FIELD)
@PrimaryKeyJoinColumn(name = "ID_PERSON", referencedColumnName = "ID")
public class SpecialWorker
extends Person
{
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID", updatable = false, insertable = false)
private Integer id01;
@ManyToOne
@JoinColumn(name = "ID_SPECIALDEPARTEMENT", referencedColumnName = "ID", nullable = false)
private SpecialDepartement specialdepartement;