数据库表
用户表(User)
id | username | password |
客户表(Customer),客户表id引用用户表id
id | phone |
员工表(Employee),员工表id引用用户表id
id |
飞机表(Plane),飞机表包含客机和战斗机,客机有厕所,战斗机有武器
id | type | speed | wc_position | weapon_position |
实体映射
//子类共享属性id,version @MappedSuperclass public abstract class IdentifiedEntity implements Serializable { @Id @GeneratedValue @Getter private Long id; @Version @Getter private int version; private void setId(Long id) { this.id = id; } private void setVersion(int version) { this.version = version; } } // User的id是在父类IdentifiedEntity中定义的 @Entity public class User extends IdentifiedEntity { @Getter @Setter private String username; @Getter @Setter private String password; } // Customer,Employee是User的子类 // 它们的id既是主键也是外键 // 它们的主键都作为外键引用User表的主键 @Entity @PrimaryKeyJoinColumn(name = "id") public class Customer extends User { @Getter @Setter private String phone; } @Entity @PrimaryKeyJoinColumn(name = "id") public class Employee extends User { @Getter @Setter private String qq; } // Plane是A370和J10的父类 @Entity @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name="type", discriminatorType=DiscriminatorType.STRING) public class Plane extends IdentifiedEntity{ private String speed; } @Entity @DiscriminatorValue("370") public class A370 extends Plane{ private String wc_position; } @Entity @DiscriminatorValue("738") public class j10 extends Plane{ private String weapon_position; }
时间: 2024-11-10 07:24:14