英文:
How to implement map association in entity
问题
我尝试在我的实体中实现映射(Map),但我不知道如何做:
``` java
public class Match {
private Long id;
@Builder.Default
@ManyToMany(targetEntity = Coupon.class,
mappedBy = "matchList")
private List<Coupon> couponList = new ArrayList<>();
}
public class Coupon {
private Long id;
@ManyToMany
@JoinTable(
name = "JOIN_MATCH_ID",
joinColumns = {@JoinColumn(name = "MATCH_ID", referencedColumnName = "ID")},
inverseJoinColumns = {@JoinColumn(name = "COUPON_ID", referencedColumnName = "ID")})
private Map<Match, Integer> mapList = new HashMap<>();
我应该如何实现这些字段?
<details>
<summary>英文:</summary>
I try to implement map in my entity but I don't know how:
``` java
public class Match {
private Long id;
@Builder.Default
@ManyToMany(targetEntity = Coupon.class,
mappedBy = "matchList")
private List<Coupon> couponList = new ArrayList<>();
}
public class Coupon {
private Long id;
@ManyToMany
@JoinTable(
name = "JOIN_MATCH_ID",
joinColumns = {@JoinColumn(name = "MATCH_ID", referencedColumnName = "ID")},
inverseJoinColumns = {@JoinColumn(name = "COUPON_ID", referencedColumnName = "ID")})
private Map<Match, Integer> mapList = new HashMap<>();
How should I implement those fields?
答案1
得分: 0
作为可能的方法之一,您可以使用以下映射。
假设您有以下数据库架构:
create table TST_MATCH
(
mt_id int not null,
primary key (mt_id)
);
create table TST_COUPON
(
cn_id int not null,
primary key (cn_id)
);
create table TST_MATCH_COUPON
(
match_id int not null,
coupon_id int not null,
UNIQUE (match_id, coupon_id),
foreign key (match_id) references TST_MATCH(mt_id),
foreign key (coupon_id) references TST_COUPON(cn_id)
);
您的映射可以像这样:
@Entity
@Table(name = "TST_MATCH")
public class Match
{
@Id
@Column(name = "mt_id")
private Integer id;
@ManyToMany(mappedBy = "matchs", cascade = CascadeType.ALL)
private List<Coupon> coupons;
// ...
}
@Entity
@Table(name = "TST_COUPON")
public class Coupon
{
@Id
@Column(name = "cn_id")
private Integer id;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(
name = "TST_MATCH_COUPON",
inverseJoinColumns = { @JoinColumn(name = "match_id", referencedColumnName = "mt_id") },
joinColumns = { @JoinColumn(name = "coupon_id", referencedColumnName = "cn_id") }
)
@MapKey(name = "id")
private Map<Integer, Match> matchs;
// ...
}
英文:
As one of the possible approaches you can use the following mapping.
Assuming that you have the following database schema:
create table TST_MATCH
(
mt_id int not null,
primary key (mt_id)
);
create table TST_COUPON
(
cn_id int not null,
primary key (cn_id)
);
create table TST_MATCH_COUPON
(
match_id int not null,
coupon_id int not null,
UNIQUE (match_id, coupon_id),
foreign key (match_id) references TST_MATCH(mt_id),
foreign key (coupon_id) references TST_COUPON(cn_id)
);
Your mapping can be like this:
@Entity
@Table(name = "TST_MATCH")
public class Match
{
@Id
@Column(name = "mt_id")
private Integer id;
@ManyToMany(mappedBy = "matchs", cascade = CascadeType.ALL)
private List<Coupon> coupons;
// ...
}
@Entity
@Table(name = "TST_COUPON")
public class Coupon
{
@Id
@Column(name = "cn_id")
private Integer id;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(
name = "TST_MATCH_COUPON",
inverseJoinColumns = { @JoinColumn(name = "match_id", referencedColumnName = "mt_id") },
joinColumns = { @JoinColumn(name = "coupon_id", referencedColumnName = "cn_id") }
)
@MapKey(name = "id")
private Map<Integer, Match> matchs;
// ...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论