简体   繁体   中英

Persist base and subclass with hibernate

I' currently trying to build a complete Spring Boot Rest service with jdbc connection by myself.

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:

@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.

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.

Hibernate can use the following strategies:

  • 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.

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 )
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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