简体   繁体   English

JPA外键到多个表

[英]JPA foreign key to multiple tables

We have a table in the database which has the following structure: 我们数据库中的表具有以下结构:

referenceId - int foreign key to various tables
tableReference - String defining the table

Is this good design, and is it somehow possible to map this relation? 这是一个好的设计,并且可以以某种方式映射这种关系吗?

This is very few information to advise you any solution, but JPA Joined Inheritance works something like that: 这些信息很少能为您提供任何解决方案的建议,但是JPA Joined Inheritance的工作原理如下:

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="PROJ_TYPE")
@Table(name="PROJECT")
public abstract class Project {
  @Id
  private long id;
  ...
}

@Entity
@DiscriminatorValue("L")
@Table(name="LARGEPROJECT")
public class LargeProject extends Project {
  private BigDecimal budget;
}

@Entity
@DiscriminatorValue("S")
@Table(name="SMALLPROJECT")
  public class SmallProject extends Project {
}

This will manage three tables in DB where the main table PROJECT which contain a column PROJ_TYPE to identify the target table. 这将管理数据库中的三个表,其中主表PROJECT包含一列PROJ_TYPE来标识目标表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM