0

我有具有复杂映射的实体,我无法正确注释。

以下是实体:

@Entity
@Table(name = "bank")
class Bank {
    @Column(name = "type")
    int type

    @ElementCollection
    @CollectionTable(name = "bank_fp", joinColumns = @JoinColumn(name = "bank_id"))
    @MapKeyColumn(name = "finance_percent")
    Map<Double, BankRates>
}

? embeddable or entity ?
class BankRates {
    @ElementCollection
    @CollectionTable(name = "bankrates", joinColumns = @JoinColumn(name = "bfp_id"))
    @MapKeyColumn(name = "track_class")
    Map<String, TrackRates>
}

? embeddable or entity ?
class TrackRates {
    ? how to map ?
    Map<YearsRange, Double>
}

@Embeddable
@Access(AccessType.FIELD)
class YearsRange {
    @Column(name = "from")
    int from
    @Column(name = "to")
    int to
}

在我看来,这些是表格:

Bank: id, type
bank_fp: id, bank_id, finance_percent - table for the map in the bank entity
bank_rates: bfp_id, track_class, from, to, rate - table for BankRates, TrackRates, YearsRange all embedded in a single table cuz its only mappings

那些是hibernate映射高手的请帮助。

4

1 回答 1

0

@entity class BankRates因为它有自己的 id

因为bank_rates包含很多 (bfp_id, track_class) 的重复项,所以您不能像这样映射它,您必须自己实现映射逻辑

class BankRates {
    @ElementCollection
    @CollectionTable(name = "bankrates", joinColumns = @JoinColumn(name = "bfp_id"))
    List<TrackRate>

}

@Embeddable
class TrackRate {
    @Column(name = "track_class")
    String trackClass;
    YearsRange
    Double
}

@Embeddable
@Access(AccessType.FIELD)
class YearsRange {
    @Column(name = "from")
    int from
    @Column(name = "to")
    int to
}
于 2012-05-25T06:37:11.840 回答