Spring Data Jpa - Entity Inheritance

JPA inheritance method

In JPA, there are three kinds of mapping strategies for entity inheritance relationship: single table inheritance strategy (SINGLE_TABLE), Joined strategy and Table_PER_Class strategy.

Associative Annotation

  1. @InheritanceUsed to configure the parent class
  • InheritanceType.SINGLE_TABLESingle table mode: All fields of all implementation classes are mapped to a table.
  • InheritanceType.TABLE_PER_CLASSIndependent table mode: Map the fields of the combined base class of each implementation class into a separate table, and each table is independent and unrelated.
  • InheritanceType.JOINEDAssociation table mode: Map the base class and each implementation class to a separate table, and use the primary key to associate, and the implementation class only contains its own unique fields.
  1. @DiscriminatorColumn(name = "member_type")It is used as a field for implementing class distinction identification. If no name is specified, a new dtype field will be created automatically. This field will be automatically assigned by the system and does not need to be specified manually.

  2. @DiscriminatorValue("wexin") Realize the identification value of class distinction, jpa will persist the data to the corresponding table according to the specific identification value, and the query statement can also automatically identify the type

operate

  • All implementation classes can be regarded as a whole, you can directly use the properties of a subclass as query conditions, you can have the return value of the subclass, or you can set the return value of the base class.
  • If the return value is the base class Member, jpa will return the proxy class of Hibernate, and you need Hibernate.unproxy(member)to get the specific implementation class
  • If the return value is the implementation class, it can be used directly
  • For write and delete operations, jpa is regarded as a whole, and the default method of memberRepository can be used directly

How the associated table schema preserves audit fields or fields with the same name

In the associated table mode, Jpa does not manage the fields in the subclass with the same name as the parent class, so that the custom soft delete field will not take effect, and the corresponding table field will not be generated. To solve this problem, you can create an attribute with a different name , which is mapped to the specified field to implement the function.

/**
 * 计量容器基础表
 * @author xxm
 * @date 2020/7/22
 */
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "mms_bd_container_base")
@DiscriminatorColumn(name = "containerType")
@SQLDelete(sql = "update mms_bd_container_base set deleted=" + AppConst.FLAG_DELETED + " where id=?")
@Where(clause = "deleted=" + AppConst.FLAG_NOT_DELETED)
public class ContainerDo extends BaseEntity{
    /** 编号 */
    @Column(unique = true)
    private String no;
    /** 容器类型 */
    @Column(insertable = false,updatable = false)
    private String containerType;
    /** 容器状态 */
    private String containerStatus;
    /** 识别码 */
    private String identifier;
    /** 识别码类型 */
    private String identifierType;
}
/**
 * 容器(车辆)
 * @author xxm
 * @date 2020/7/22
 */
@Entity
@Table(name = "mms_bd_container_vehicle")
@DiscriminatorValue("vehicle")
@SQLDelete(sql = "update mms_bd_container_vehicle set deleted=" + AppConst.FLAG_DELETED + " where id=?")
@Where(clause = "deleted=" + AppConst.FLAG_NOT_DELETED)
public class ContainerVehicleDo extends ContainerDo implements EntityBaseFunction<containervehicledto> {
    /** 车辆名称 */
    private String vehicleName;
    /** 重量 */
    private Double weight;
    /** 计量时间 */
    private LocalDateTime weighingTime;
    /** 重量有效期类型 */
    private String validPeriod;
    /** 删除标志(父类同名属性) */
    @Column(name = "deleted")
    private boolean deleted0;
}

Project Recommendation: Based on Spring Cloud Alibaba technology, a distributed microservice development platform including basic functions, OA, payment, marketing and other modules

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324275793&siteId=291194637