如何在实体中实现地图关联

huangapple go评论85阅读模式
英文:

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&#39;t know how:

``` java
public class Match {
private Long id;

    @Builder.Default
    @ManyToMany(targetEntity = Coupon.class,
            mappedBy = &quot;matchList&quot;)
    private List&lt;Coupon&gt; couponList = new ArrayList&lt;&gt;();
}
public class Coupon {
private Long id;

    @ManyToMany
    @JoinTable(
            name = &quot;JOIN_MATCH_ID&quot;,
            joinColumns = {@JoinColumn(name = &quot;MATCH_ID&quot;, referencedColumnName = &quot;ID&quot;)},
            inverseJoinColumns = {@JoinColumn(name = &quot;COUPON_ID&quot;, referencedColumnName = &quot;ID&quot;)})
    private Map&lt;Match, Integer&gt; mapList = new HashMap&lt;&gt;();

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 = &quot;TST_MATCH&quot;)
public class Match
{
   @Id
   @Column(name = &quot;mt_id&quot;)
   private Integer id;
   
   @ManyToMany(mappedBy = &quot;matchs&quot;, cascade = CascadeType.ALL)
   private List&lt;Coupon&gt; coupons;
   
   // ...
}

@Entity
@Table(name = &quot;TST_COUPON&quot;)
public class Coupon
{
   @Id
   @Column(name = &quot;cn_id&quot;)
   private Integer id;

   @ManyToMany(cascade = CascadeType.ALL)
   @JoinTable(
      name = &quot;TST_MATCH_COUPON&quot;,
      inverseJoinColumns = { @JoinColumn(name = &quot;match_id&quot;, referencedColumnName = &quot;mt_id&quot;) },
      joinColumns = { @JoinColumn(name = &quot;coupon_id&quot;, referencedColumnName = &quot;cn_id&quot;) }
   )
   @MapKey(name = &quot;id&quot;)
   private Map&lt;Integer, Match&gt; matchs;
   
   // ...
}

huangapple
  • 本文由 发表于 2020年8月22日 04:36:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63529764.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定