Hibernate Polymorphism和连接表

时间:2016-02-03 13:57:47

标签: java hibernate inheritance

Database Schema 从图像中描述的数据库结构开始,尝试在实体" Configuration_ext"中创建Getter和Setter方法。这样我就可以管理硬盘和机箱数据而无需通过" conf_component"和"组件"。

这是我的对象映射:

@Getter
@Setter
@Entity
@Inheritance(strategy= InheritanceType.JOINED)
@DiscriminatorColumn(name="COMP_TYPE")
@Table(name="COMPONENT")
public abstract class BaseComponent {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    @Column(name = "ID", unique = true, nullable = false)
    protected Long  id;
    @Column(name = "CODE", unique = true, nullable = false)
    protected String    code;
    @Column(name = "DESCRIPTION", nullable = false)
    protected String    description;

    @Column(name = "COMP_TYPE", nullable = false)
    protected String    compType;

    @OneToMany(mappedBy = "baseComponent", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
    protected List<ConfComponent> confsHdd;
}

@Getter
@Setter
@Entity
@DiscriminatorValue("H")
@Table(name="HDD_EXT")
public class HddExt extends BaseComponent {

    @Column(name = "SIZE",nullable = false)
    private String  size;
    @Column(name = "BUS",nullable = false)
    private String  bus;
    @Column(name = "TYPE", nullable = false)
    private String  type;
    @Column(name = "FORM_FACTOR", nullable = false)
    private String  formFactor;
    @Column(name = "RPM", nullable = true)
    private String  rpm;
}

@Getter
@Setter
@Entity
@DiscriminatorValue("C")
@Table(name="CHASSIS_EXT")
public class ChassisExt extends BaseComponent {

    @Column(name = "MANUFACTOR",nullable = false)
    private String manufactor;
    @Column(name = "CPU_MULTIPLIER",nullable = false)
    private String  cpuMultiplier;
    @Column(name = "HDD_MULTIPLIER",nullable = false)
    private String  hddMultiplier;
    @Column(name = "HDD_TYPES", nullable = false)
    private String  hddTypes;
    @Column(name = "RAM_TYPES", nullable = false)
    private String  ramTypes;
}

@Getter
@Setter
@Entity
@Table(name = "CONF_COMPONENT")
public class ConfComponent {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    @Column(name = "ID", unique = true, nullable = false)
    protected Long  id;
    @Column(name = "QUANTITY", nullable = false)
    protected Integer quantity;
    @Column(name = "SORTING", nullable = false)
    protected Integer sorting;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ID_COMPONENT")
    private BaseComponent   baseComponent;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ID_CONFIGURATION")
    protected Configuration configuration;
}

@Getter
@Setter
@Entity
@Table(name = "CONFIGURATION_EXT")
public class ConfigurationExt {
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    @Column(name = "ID", unique = true, nullable = false)
    protected Long  id;
    @Column(name = "CODE", unique = true, nullable = false)
    protected String    code;
    @Column(name = "DESCRIPTION",nullable = false)
    protected String    description;
    @Column(name = "CATEGORY",nullable = false)
    private String  category;

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
    @JoinTable(name="CONF_COMPONENTS",
            joinColumns={@JoinColumn(name="ID_CONFIGURATION", referencedColumnName="id")},
            inverseJoinColumns={@JoinColumn(name="ID_COMPONENT", referencedColumnName="id")})
    @Where(clause = "code = 'HDD_1'")
    private List<BaseComponent> confsHdd;
}

1 个答案:

答案 0 :(得分:0)

@MappedSuperclass注释hibernate支持实体在超类中保留基本属性。我也认为这个类应该为oop概念提供封装

@Getter
@Setter
@Entity
@Inheritance(strategy= InheritanceType.JOINED)
@DiscriminatorColumn(name="COMP_TYPE")
@Table(name="COMPONENT")
@MappedSuperclass
public class BaseComponent {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    @Column(name = "ID", unique = true, nullable = false)
    private Long  id;
    @Column(name = "CODE", unique = true, nullable = false)
    private String    code;
    @Column(name = "DESCRIPTION", nullable = false)
    private String    description;

    @Column(name = "COMP_TYPE", nullable = false)
    private String    compType;

    @OneToMany(mappedBy = "baseComponent", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
    private List<ConfComponent> confsHdd;
}

@Getter
@Setter
@Entity
@DiscriminatorValue("H")
@Table(name="HDD_EXT")
public class HddExt extends BaseComponent {

    @Column(name = "SIZE",nullable = false)
    private String  size;
    @Column(name = "BUS",nullable = false)
    private String  bus;
    @Column(name = "TYPE", nullable = false)
    private String  type;
    @Column(name = "FORM_FACTOR", nullable = false)
    private String  formFactor;
    @Column(name = "RPM", nullable = true)
    private String  rpm;
}

@Getter
@Setter
@Entity
@DiscriminatorValue("C")
@Table(name="CHASSIS_EXT")
public class ChassisExt extends BaseComponent {

    @Column(name = "MANUFACTOR",nullable = false)
    private String manufactor;
    @Column(name = "CPU_MULTIPLIER",nullable = false)
    private String  cpuMultiplier;
    @Column(name = "HDD_MULTIPLIER",nullable = false)
    private String  hddMultiplier;
    @Column(name = "HDD_TYPES", nullable = false)
    private String  hddTypes;
    @Column(name = "RAM_TYPES", nullable = false)
    private String  ramTypes;
}

@Getter
@Setter
@Entity
@Table(name = "CONF_COMPONENT")
public class ConfComponent {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    @Column(name = "ID", unique = true, nullable = false)
    private Long  id;
    @Column(name = "QUANTITY", nullable = false)
    private Integer quantity;
    @Column(name = "SORTING", nullable = false)
    private Integer sorting;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ID_COMPONENT")
    private BaseComponent   baseComponent;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ID_CONFIGURATION")
    private Configuration configuration;
}

@Getter
@Setter
@Entity
@Table(name = "CONFIGURATION_EXT")
public class ConfigurationExt {
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    @Column(name = "ID", unique = true, nullable = false)
    private Long  id;
    @Column(name = "CODE", unique = true, nullable = false)
    private String    code;
    @Column(name = "DESCRIPTION",nullable = false)
    private String    description;
    @Column(name = "CATEGORY",nullable = false)
    private String  category;

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
    @JoinTable(name="CONF_COMPONENTS",
            joinColumns={@JoinColumn(name="ID_CONFIGURATION", referencedColumnName="id")},
            inverseJoinColumns={@JoinColumn(name="ID_COMPONENT", referencedColumnName="id")})
    @Where(clause = "code = 'HDD_1'")
    private List<BaseComponent> confsHdd;
}