简体   繁体   English

使用休眠保持基类和子类

[英]Persist base and subclass with hibernate

I' currently trying to build a complete Spring Boot Rest service with jdbc connection by myself.我目前正在尝试通过自己的 jdbc 连接构建一个完整的 Spring Boot Rest 服务。

At the moment I'm struggling with a minor problem of comprehension regarding hibernate and storing entities.目前,我正在努力解决有关休眠和存储实体的理解的小问题。

I have one base class:我有一个基类:

@Entity
@Table
public abstract class Person {

  private int id;
  private String firstName;
  private String middleName;
  private String lastName;

  @Id
  @GeneratedValue(generator = "increment")
  @GenericGenerator(name = "increment", strategy = "increment")
  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  @Column
  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  @Column
  public String getMiddleName() {
    return middleName;
  }

  public void setMiddleName(String middleName) {
    this.middleName = middleName;
  }

  @Column
  public String getLastName() {
    return lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }
}

And 2 sub classes:和 2 个子类:

@Entity
@Table
public class Member extends Person{

  private String memberNumber;

  @Column
  public String getMemberNumber() {
    return memberNumber;
  }

  public void setMemberNumber(String memberNumber) {
    this.memberNumber = memberNumber;
  }
}

and

@Entity
@Table
public class Supporter extends Person {

  private String supporterNumber;

  @Column
  public String getSupporterNumber() {
    return supporterNumber;
  }

  public void setSupporterNumber(String supporterNumber) {
    this.supporterNumber = supporterNumber;
  }
}

The base class is abstract because I want to prevent to create a instance of this without specify a person either a member or supporter.基类是抽象的,因为我想防止在没有指定成员或支持者的情况下创建它的实例。 But in the database scheme I still want to have 3 tables because of normalization.但是在数据库方案中,由于规范化,我仍然希望有 3 个表。

Which annotations should I use now the reach this target?我现在应该使用哪些注释来达到这个目标? How can I link a row of member or supporter to the member now?我现在如何将一行成员或支持者链接到该成员? I'm really confused.我真的很困惑。

Thanks!谢谢!

The mapping from class to table is done by hibernate.从类到表的映射是由休眠完成的。 Since a relational database table and a Java object are kinda different ORM mappers have different strategies how to map between them.由于关系数据库表和 Java 对象有点不同,ORM 映射器有不同的策略如何在它们之间进行映射。

Hibernate can use the following strategies: Hibernate 可以使用以下策略:

  • MappedSuperclass映射超类
  • Single Table (default)单表(默认)
  • Joined Table连接表
  • Table per class每班表

You can read more about them from the official documentation .您可以从官方文档中阅读更多关于它们的信息

They have different pros and cons and normally it is safest to just use the default.它们有不同的优点和缺点,通常使用默认值是最安全的。 However the default strategy only uses one table so you need to switch to an other strategy.但是默认策略仅使用一个表,因此您需要切换到其他策略。

The Table per class will create three tables.每个类的表将创建三个表。 You can also check the examples for MappedSuperclass and Joined Table which will also use multiple tables.您还可以查看 MappedSuperclass 和 Joined Table 的示例,它们也将使用多个表。

From the official documentation:来自官方文档:

@Entity(name = "Account")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public static class Account {
    @Id
    private Long id;
    private String owner;
    private BigDecimal balance;
    private BigDecimal interestRate;
    //Getters and setters are omitted for brevity
}

@Entity(name = "DebitAccount")
public static class DebitAccount extends Account {
    private BigDecimal overdraftFee;
    //Getters and setters are omitted for brevity
}

@Entity(name = "CreditAccount")
public static class CreditAccount extends Account {
    private BigDecimal creditLimit;
    //Getters and setters are omitted for brevity
}

Will create these tables:将创建这些表:

CREATE TABLE Account (
    id BIGINT NOT NULL ,
    balance NUMERIC(19, 2) ,
    interestRate NUMERIC(19, 2) ,
    owner VARCHAR(255) ,
    PRIMARY KEY ( id )
)

CREATE TABLE CreditAccount (
    id BIGINT NOT NULL ,
    balance NUMERIC(19, 2) ,
    interestRate NUMERIC(19, 2) ,
    owner VARCHAR(255) ,
    creditLimit NUMERIC(19, 2) ,
    PRIMARY KEY ( id )
)

CREATE TABLE DebitAccount (
    id BIGINT NOT NULL ,
    balance NUMERIC(19, 2) ,
    interestRate NUMERIC(19, 2) ,
    owner VARCHAR(255) ,
    overdraftFee NUMERIC(19, 2) ,
    PRIMARY KEY ( id )
)

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

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