SpringDataJPA笔记(14)-Inheritance注解详解之SINGLE_TABLE
在JPA中使用映射注解Inheritance,有三种策略属性
SINGLE_TABLE – 将所有父类和子类集合在一张表
TABLE_PER_CLASS – 每个子类会生成一张单独的表,父类可以查询所有子类的表数据,参考上一篇笔记Union查询
JOINED – 每个类分别生成一张单独的表,但是每张表只有自己的属性,没有父类的属性,通过外键关联的形式使两张表关联起来
public enum InheritanceType {
/** A single table per class hierarchy. */
SINGLE_TABLE,
/** A table per concrete entity class. */
TABLE_PER_CLASS,
/**
* A strategy in which fields that are specific to a
* subclass are mapped to a separate table than the fields
* that are common to the parent class, and a join is
* performed to instantiate the subclass.
*/
JOINED
}
简单写个demo测试一下
分别新建三个实体类
@Data
@Table(name = "inheritance_single_tb")
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class InheritanceSingleEntity implements Serializable {
private static final long serialVersionUID = 8139674112977338603L;
@Id
private Long id;
@Column(name = "single_name")
private String singleName;
}
@Data
@Entity
@EqualsAndHashCode(callSuper = true)
@Table(name = "inheritance_single_one_tb")
public class InheritanceSingleOneEntity extends InheritanceSingleEntity{
private static final long serialVersionUID = 9042160117012026124L;
@Column(name = "single_one")
private String singleOne;
}
@Data
@Entity
@EqualsAndHashCode(callSuper = true)
@Table(name = "inheritance_single_two_tb")
public class InheritanceSingleTwoEntity extends InheritanceSingleEntity {
private static final long serialVersionUID = 9042160117012026124L;
@Column(name = "single_two")
private String singleTwo;
}
再新建三个repository接口类
public interface InheritanceSingleRepository extends JpaRepository, JpaSpecificationExecutor, Serializable {
}
public interface InheritanceSingleOneRepository extends JpaRepository, JpaSpecificationExecutor, Serializable {
}
public interface InheritanceSingleTwoRepository extends JpaRepository, JpaSpecificationExecutor, Serializable {
}
新建一个controller类用于测试数据情况
@RestController
@RequestMapping("/chapter/inheritances")
public class ChapterInheritanceController {
@Autowired
private InheritanceSingleRepository inheritanceSingleRepository;
@Autowired
private InheritanceSingleOneRepository inheritanceSingleOneRepository;
@Autowired
private InheritanceSingleTwoRepository inheritanceSingleTwoRepository;
@PostMapping("/single")
public InheritanceSingleEntity single(@RequestBody InheritanceSingleEntity inheritanceSingleEntity){
return inheritanceSingleRepository.save(inheritanceSingleEntity);
}
@PostMapping("/single/one")
public InheritanceSingleOneEntity single(@RequestBody InheritanceSingleOneEntity inheritanceSingleEntity){
return inheritanceSingleOneRepository.save(inheritanceSingleEntity);
}
@PostMapping("/single/two")
public InheritanceSingleTwoEntity single(@RequestBody InheritanceSingleTwoEntity inheritanceSingleEntity){
return inheritanceSingleTwoRepository.save(inheritanceSingleEntity);
}
@GetMapping("/single")
public List single(){
return inheritanceSingleRepository.findAll();
}
@GetMapping("/single/one")
public List singleOne(){
return inheritanceSingleOneRepository.findAll();
}
@GetMapping("/single/two")
public List singleTwo(){
return inheritanceSingleTwoRepository.findAll();
}
}
此时查看数据库,看到生成了一张表
查看表结构,发现有三张表的字段同时存在
在swagger页面分别通过三个接口创建三条数据,查看表数据
例如加上@DiscriminatorColumn加@DiscriminatorValue的注解
@Data
@Table(name = "inheritance_single_tb1")
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "single_type", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue(value = "base")
public class InheritanceSingleEntity implements Serializable {
private static final long serialVersionUID = 8139674112977338603L;
@Id
private Long id;
@Column(name = "single_name")
private String singleName;
}
@Data
@Entity
@EqualsAndHashCode(callSuper = true)
@Table(name = "inheritance_single_one_tb")
@DiscriminatorValue(value = "one")
public class InheritanceSingleOneEntity extends InheritanceSingleEntity{
private static final long serialVersionUID = 9042160117012026124L;
@Column(name = "single_one")
private String singleOne;
}
@Data
@Entity
@EqualsAndHashCode(callSuper = true)
@Table(name = "inheritance_single_two_tb")
@DiscriminatorValue(value = "two")
public class InheritanceSingleTwoEntity extends InheritanceSingleEntity {
private static final long serialVersionUID = 9042160117012026124L;
@Column(name = "single_two")
private String singleTwo;
}
写入三条数据之后查看数据库
再通过查询接口查询数据
可以看到通过父类查询可以查询所有数据,而子类只能分别查询出子类的数据
欢迎关注微信交流