SpringDataJPA笔记(15)--Inheritance注解详解之JOINED

SpringDataJPA笔记(15)–Inheritance注解详解之JOINED

在JPA中使用映射注解Inheritance,有一种策略是JOINED

JOINED – 每个类分别生成一张单独的表,但是每张表只有自己的属性,没有父类的属性,通过外键关联的形式使两张表关联起来

还是简单写个demo测试一下

先写三个实体类

@Data
@Table(name = "inheritance_joined_tb")
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class InheritanceJoinedEntity implements Serializable {
    private static final long serialVersionUID = 8139674112977338603L;

    @Id
    private Long id;

    @Column(name = "joined_name")
    private String joinedName;
}

@Data
@Entity
@EqualsAndHashCode(callSuper = true)
@Table(name = "inheritance_joined_one_tb")
public class InheritanceJoinedOneEntity extends InheritanceJoinedEntity {

    private static final long serialVersionUID = 9042160117012026124L;

    @Column(name = "joined_one")
    private String joinedOne;
}
@Data
@Entity
@EqualsAndHashCode(callSuper = true)
@Table(name = "inheritance_joined_two_tb")
public class InheritanceJoinedTwoEntity extends InheritanceJoinedEntity {

    private static final long serialVersionUID = 9042160117012026124L;

    @Column(name = "joined_two")
    private String joinedTwo;
}

然后是三个repository接口类

public interface InheritanceJoinedRepository extends JpaRepository, JpaSpecificationExecutor, Serializable {
}
public interface InheritanceJoinedOneRepository extends JpaRepository, JpaSpecificationExecutor, Serializable {
}
public interface InheritanceJoinedTwoRepository extends JpaRepository, JpaSpecificationExecutor, Serializable {
}

然后写controller类用于测试

@Autowired
    private InheritanceJoinedRepository inheritanceJoinedRepository;

    @Autowired
    private InheritanceJoinedOneRepository inheritanceJoinedOneRepository;

    @Autowired
    private InheritanceJoinedTwoRepository inheritanceJoinedTwoRepository;

    @PostMapping("/single")
    public InheritanceJoinedEntity single(@RequestBody InheritanceJoinedEntity inheritanceJoinedEntity) {
        return inheritanceJoinedRepository.save(inheritanceJoinedEntity);
    }

    @PostMapping("/joined/one")
    public InheritanceJoinedOneEntity joined(@RequestBody InheritanceJoinedOneEntity inheritanceJoinedEntity) {
        return inheritanceJoinedOneRepository.save(inheritanceJoinedEntity);
    }

    @PostMapping("/joined/two")
    public InheritanceJoinedTwoEntity joined(@RequestBody InheritanceJoinedTwoEntity inheritanceJoinedEntity) {
        return inheritanceJoinedTwoRepository.save(inheritanceJoinedEntity);
    }

    @GetMapping("/joined")
    public List joined() {
        return inheritanceJoinedRepository.findAll();
    }

    @GetMapping("/joined/one")
    public List joinedOne() {
        return inheritanceJoinedOneRepository.findAll();
    }

    @GetMapping("/joined/two")
    public List joinedTwo() {
        return inheritanceJoinedTwoRepository.findAll();
    }

启动程序,查看数据库,有三张表
在这里插入图片描述

分别通过三个接口写入数据,再查看数据库数据
SpringDataJPA笔记(15)--Inheritance注解详解之JOINED_第1张图片
SpringDataJPA笔记(15)--Inheritance注解详解之JOINED_第2张图片
SpringDataJPA笔记(15)--Inheritance注解详解之JOINED_第3张图片

通过接口查询数据的结果
SpringDataJPA笔记(15)--Inheritance注解详解之JOINED_第4张图片

SpringDataJPA笔记(15)--Inheritance注解详解之JOINED_第5张图片
SpringDataJPA笔记(15)--Inheritance注解详解之JOINED_第6张图片

欢迎关注微信交流
在这里插入图片描述

你可能感兴趣的:(springboot,jpa)