I have three tables which has two identical column (i.e. LAST_MODIFIED_BY and LAST_MODIFIED_DATETIME).
How do define entities via entity inheritance in JPA?
How-To
There are two steps to implement entity inheritance:
[Step 1] Extract the two columns into an abstract class with @MappedSuperclass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | package test.entity; import java.io.Serializable; import java.util.Date; import javax.persistence.Column; import javax.persistence.MappedSuperclass; import org.springframework.data.annotation.LastModifiedBy; import org.springframework.data.annotation.LastModifiedDate; import lombok.Data; @Data @MappedSuperclass public abstract class CommonEntity implements Serializable { private static final long serialVersionUID = 1L; @LastModifiedBy @Column(name = "LAST_MODIFIED_BY") private String lastModifiedBy; @LastModifiedDate @Column(name = "LAST_MODIFIED_DATETIME") private Date lastModifiedDatetime; } |
[Step 2] Extend the abstract class from the three entities:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | package test.entity; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.EntityListeners; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.SequenceGenerator; import javax.persistence.Table; import org.springframework.data.jpa.domain.support.AuditingEntityListener; import lombok.Data; import lombok.EqualsAndHashCode; @Entity @Table(name = "PROJECT") @Data @EqualsAndHashCode(of = "id", callSuper = false) public class Project extends CommonEntity implements Serializable { private static final long serialVersionUID = 1L; @Id @SequenceGenerator(name = "PROJECT_ID_SEQ", sequenceName = "PROJECT_ID_SEQ", allocationSize = 1) @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "PROJECT_ID_SEQ") @Column(name = "ID") private Integer id; @Column(name = "IDENTIFIER") private String identifier; @Column(name = "NAME") private String name; @Column(name = "STATUS") private String status; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | package test.entity; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.EntityListeners; import javax.persistence.Id; import javax.persistence.Lob; import javax.persistence.Table; import org.springframework.data.jpa.domain.support.AuditingEntityListener; import lombok.Data; import lombok.EqualsAndHashCode; @Entity @Table(name = "MODEL") @Data @EqualsAndHashCode(of = "id", callSuper = false) public class Model extends CommonEntity implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name = "ID") private String id; @Column(name = "PROJECT_ID") private Integer projectId; @Lob @Column(name = "STATISTICS_JSON") private String statisticsJson; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | package com.cht.ai.bot.entity; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.EntityListeners; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.SequenceGenerator; import javax.persistence.Table; import org.springframework.data.jpa.domain.support.AuditingEntityListener; import lombok.Data; import lombok.EqualsAndHashCode; @Entity @Table(name = "SLOT") @Data @EqualsAndHashCode(of = "id", callSuper = false) public class Slot extends CommonEntity implements Serializable { private static final long serialVersionUID = 1L; @Id @SequenceGenerator(name = "SLOT_ID_SEQ", sequenceName = "SLOT_ID_SEQ", allocationSize = 1) @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SLOT_ID_SEQ") @Column(name = "ID") private Integer id; @Column(name = "NAME") private String name; @Column(name = "TYPE") private String type; } |
No comments:
Post a Comment