JPA InheritanceType.JOINED怎么写父类的关系,实体

问题描述 投票:0回答:1

我有三个孩子(股票,债券,ETF)父实体类(仪器)。父类也有类(InstrumentPrice)一个一对多的关系。 JPA的加盟策略,母公司和儿童实体之间使用。当一个孩子持续(如股票),然后在仪器中的条目自动与所有的公共属性坚持为好。这工作正常。但是,你如何创建的类InstrumentPrice的入口?我必须有一个持续后股价读取来自DB实体仪器,并与InstrumentPrice更新工具?

@MappedSuperclass
public abstract class BaseEntity implements Serializable {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    protected int id;

}

父类仪器

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "instr_type", discriminatorType = DiscriminatorType.STRING, length = 1)
@Table(name = "instrument")

public class Instrument extends BaseEntity {

    @Column
    private String symbol;

    @Column
    private String isin;

    @Column
    private String name;

    @Column
    private boolean active;

    @OneToMany(mappedBy = "instrument", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private List<InstrumentPrice> instrumentPriceList;

    // public getters & setters
    ....

类InstrumentPrice

@Entity
@Table(name = "instrumentprice")
public class InstrumentPrice extends BaseEntity {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "instrument_id", nullable = false)
    private Instrument instrument;

    @Column
    private LocalDate date;
    @Column
    private double open;
    @Column
    private double high;
    @Column
    private double low;
    @Column
    private double close;
    @Column
    private long volume;

    // public getters & setters
    ....

子类股票

@Entity
@DiscriminatorValue("S")
@PrimaryKeyJoinColumn(name = "id")
@Table(name = "stock")
public class Stock extends Instrument { 

...  fields specific to Stock ...

}
java hibernate jpa inheritance
1个回答
0
投票

你必须设置关系的两侧。在仪器类中创建添加instrumentPrice实体法。

public void addInstrumentPrice(ip){
   instrumentPriceList.add(ip);
   ip.setInstrument(this);
}
© www.soinside.com 2019 - 2024. All rights reserved.