用于实现以下要求的最佳JPA继承选项

时间:2018-04-23 03:38:37

标签: jpa inheritance jpa-2.0 jpa-2.1

拥有以下类型的表格表。坚持这些表格的好方法是什么?为此使用了继承策略,但它没有按预期工作。

要求1:需要坚持学生表,它还会保留会员以及地址表

要求2:需要坚持老师表,它还会保留会员以及地址表

需要在这些表上执行get,update和delete选项。

Member {
  member_id - have one to one relation with student id and teacher id
  lastupdateddate
  latupdatedby
}

Student {
  student id - have one to one relation with member id
  student name
  lastupdateddate
  latupdatedby
}

teacher {
  teacher id - have one to one relation ship with member
  teacher name
  lastupdateddate
  latupdatedby
}

address {
  address id
  member_id - have one to one relationship with member class
  lastupdateddate
  latupdatedby
}

当我持续/更新学生详细信息时,地址相关信息未正确插入或更新。当我检查在成员上触发的插入查询时,然后学生表在地址表上。但是,在插入查询到地址表中,member_id值将变为null。因此,仅填充地址表。

Entity structure is is as given below

public abstract class Member implements Serializable {
}
public class Student extends member implements Serializable {
}
public class Teacher extends member implements Serializable {
}
public class Address implements Serializable {
}

如下所述提到映射。尝试了各种可用的选项。

In member entity class
       @Access(AccessType.PROPERTY)
       @OneToOne(mappedBy="member", cascade=CascadeType.ALL)
       public Address getAddress() {
              return postalAddress;
       }

In  address entity class
       @OneToOne(fetch=FetchType.LAZY)
       @JoinColumn(name = "MEMBER_ID")
       private Address address;

1 个答案:

答案 0 :(得分:0)

看起来你有两个选择:

  1. InheritanceType.JOINED。它几乎就是你所描述的:公共部分在一个表中,不同的部分在不同的表中。每次请求JOIN都会出现
  2. InheritanceType.SINGLE_TABLE。在这里,所有数据都将存储在单个表格中,并且将使用描述符来确定是否为学生或教师记录。
  3. 就我个人而言,我更喜欢第二种选择,因为您几乎所有字段都有共同点,而且大部分操作都较轻且涉及WHERE,而不是JOIN