如何通过链接表完成@ManyToOne映射?

时间:2019-03-16 08:20:37

标签: hibernate

我的数据库中有以下表格:

CREATE TABLE TRD_parties (
    id long NOT NULL AUTO_INCREMENT,
    name varchar(255),
    shortname varchar(16),
    PRIMARY KEY (id)
);

CREATE TABLE TRD_funds (
    id long NOT NULL AUTO_INCREMENT,
    shortname varchar(16),
    name varchar(255),
    units float,
    party_id long,
    PRIMARY KEY (id),
    FOREIGN KEY (party_id) REFERENCES TRD_parties(id)
);

CREATE TABLE TRD_funds_available4party (
    party_id long,
    fund_id long,
    FOREIGN KEY (party_id) REFERENCES TRD_parties(id),
    FOREIGN KEY (fund_id) REFERENCES TRD_funds(id)
);

1个基金仅属于一个缔约方,并且1个缔约方有一个资金清单。实体看起来像这样:

public class Fund{

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
...

@ManyToOne(cascade = CascadeType.ALL)//no problem with this mapping
@JoinColumn(name = "partyId")
private Party party;


@Entity
@Table(name = "TRD_parties")
public class Party{

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@OneToMany(fetch = FetchType.LAZY, mappedBy="availableFunds")//this is wrong how to map it?
private List<Fund> availableFunds;

我的问题就像在方实体中:如何使用映射将可用资金列表(在链接表TRD_funds_available4party中描述)分配给一方?

我发现了一些技巧here,但是首先我不知道如何使这个示例适应我的情况,其次它可以在带有Spring Boot的Hibernate 5中使用吗?

0 个答案:

没有答案