多个 类 到一个 table JPA

Multiple classes to one table JPA

是否可以根据字段的值加载不同的 类?对于这些 类 中的每一个,数据库中都会有一个对应的行,并且可以通过 "type" 字段来识别。他们将共享相同的界面(应用)。

我认为您可以使用 JPA 轻松实现这一目标。假设您有一个 "location" table 并且您可以为不同类型的位置设置不同的行,例如 StoreLocation 和 CustomerLocation。然后我们可以在 JPA 实体中对其进行建模,如下所示。

@Entity
@DiscriminatorColumn(name = "name of column you need to filter with")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class LocationEntity {

}

然后我们可以有其他更具体的tables

@Entity
@DiscriminatorValue("STORE") // set the value of the column for store location rows
public class StoreLocationEntity extends LocationEntity {

}

还有一个

@Entity
@DiscriminatorValue("CUSTOMER") // set the value of the column for customer location rows
public class CustomerLocationEntity extends LocationEntity {

}

希望这个例子能帮助您入门。