In Hibernate, an entity class can inherit from an entity class or a non-entity class. However, there is no inheritance relationship between relational database tables. So how is the inheritance relationship between the entity classes represented in the database table?
Hibernate provides 4 JPA-compatible strategies to address the mismatch between the inheritance of the entity class and the relational database table. The first type of Mappedsuperclass is described here.
In this strategy, there are the following characteristics:
There is an inheritance relationship only between the entity classes, where the parent entity class uses @javax.persistence.mappedsuperclass annotations.
There is no parent entity class in the relational database, and a specific child entity class corresponds to a table that contains all the attributes of a specific child entity class (containing the attributes of the parent entity Class).
In the example, the parent entity class is defined as follows:
@MappedSuperclasspublic Static class Account { @Id private Long Id; Private String owner; Private BigDecimal balance; Private BigDecimal interestrate; Public Long getId () { return ID; } public void SetId (Long id) { this.id = ID; } Public String GetOwner () { return owner; } public void SetOwner (String owner) { This.owner = owner; } Public BigDecimal GetBalance () { return balance; } public void Setbalance (BigDecimal balance) { this.balance = balance; } Public BigDecimal getinterestrate () { return interestrate; } public void Setinterestrate (BigDecimal interestrate) { this.interestrate = interestrate; }}
The child entity class is defined as follows:
@Entity (name = "Debitaccount") public static class Debitaccount extends account { private BigDecimal Overdraftfee;
public BigDecimal Getoverdraftfee () { return overdraftfee; } public void Setoverdraftfee (BigDecimal overdraftfee) { this.overdraftfee = Overdraftfee; }}
The other child entity class is defined as follows:
@Entity (name = "Creditaccount") public static class Creditaccount extends account { private BigDecimal creditlimit;< C8/>public BigDecimal Getcreditlimit () { return creditlimit; } public void Setcreditlimit (BigDecimal creditlimit) { this.creditlimit = Creditlimit; }}
The database table structure is as follows:
CREATE TABLE debitaccount ( ID BIGINT not NULL, balance NUMERIC (2), interestrate NUMERIC (2), ow NER VARCHAR (255), Overdraftfee NUMERIC (2), PRIMARY KEY (ID) ) CREATE TABLE creditaccount ( ID BIGINT not NULL, balance NUMERIC (2), interestrate NUMERIC (2), owner VARCHAR (255), Creditli MIT NUMERIC (2), PRIMARY KEY (ID) )
Via:http://blog.csdn.net/taiyangdao/article/details/51578386
One of the inheritance relationships between the entity classes in Hibernate Mappedsuperclass