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

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

How to implement map association in entity

问题

  1. 我尝试在我的实体中实现映射Map),但我不知道如何做
  2. ``` java
  3. public class Match {
  4. private Long id;
  5. @Builder.Default
  6. @ManyToMany(targetEntity = Coupon.class,
  7. mappedBy = "matchList")
  8. private List<Coupon> couponList = new ArrayList<>();
  9. }
  1. public class Coupon {
  2. private Long id;
  3. @ManyToMany
  4. @JoinTable(
  5. name = "JOIN_MATCH_ID",
  6. joinColumns = {@JoinColumn(name = "MATCH_ID", referencedColumnName = "ID")},
  7. inverseJoinColumns = {@JoinColumn(name = "COUPON_ID", referencedColumnName = "ID")})
  8. private Map<Match, Integer> mapList = new HashMap<>();

我应该如何实现这些字段?

  1. <details>
  2. <summary>英文:</summary>
  3. I try to implement map in my entity but I don&#39;t know how:
  4. ``` java
  5. public class Match {
  6. private Long id;
  7. @Builder.Default
  8. @ManyToMany(targetEntity = Coupon.class,
  9. mappedBy = &quot;matchList&quot;)
  10. private List&lt;Coupon&gt; couponList = new ArrayList&lt;&gt;();
  11. }
  1. public class Coupon {
  2. private Long id;
  3. @ManyToMany
  4. @JoinTable(
  5. name = &quot;JOIN_MATCH_ID&quot;,
  6. joinColumns = {@JoinColumn(name = &quot;MATCH_ID&quot;, referencedColumnName = &quot;ID&quot;)},
  7. inverseJoinColumns = {@JoinColumn(name = &quot;COUPON_ID&quot;, referencedColumnName = &quot;ID&quot;)})
  8. private Map&lt;Match, Integer&gt; mapList = new HashMap&lt;&gt;();

How should I implement those fields?

答案1

得分: 0

作为可能的方法之一,您可以使用以下映射。

假设您有以下数据库架构:

  1. create table TST_MATCH
  2. (
  3. mt_id int not null,
  4. primary key (mt_id)
  5. );
  6. create table TST_COUPON
  7. (
  8. cn_id int not null,
  9. primary key (cn_id)
  10. );
  11. create table TST_MATCH_COUPON
  12. (
  13. match_id int not null,
  14. coupon_id int not null,
  15. UNIQUE (match_id, coupon_id),
  16. foreign key (match_id) references TST_MATCH(mt_id),
  17. foreign key (coupon_id) references TST_COUPON(cn_id)
  18. );

您的映射可以像这样:

  1. @Entity
  2. @Table(name = "TST_MATCH")
  3. public class Match
  4. {
  5. @Id
  6. @Column(name = "mt_id")
  7. private Integer id;
  8. @ManyToMany(mappedBy = "matchs", cascade = CascadeType.ALL)
  9. private List<Coupon> coupons;
  10. // ...
  11. }
  12. @Entity
  13. @Table(name = "TST_COUPON")
  14. public class Coupon
  15. {
  16. @Id
  17. @Column(name = "cn_id")
  18. private Integer id;
  19. @ManyToMany(cascade = CascadeType.ALL)
  20. @JoinTable(
  21. name = "TST_MATCH_COUPON",
  22. inverseJoinColumns = { @JoinColumn(name = "match_id", referencedColumnName = "mt_id") },
  23. joinColumns = { @JoinColumn(name = "coupon_id", referencedColumnName = "cn_id") }
  24. )
  25. @MapKey(name = "id")
  26. private Map<Integer, Match> matchs;
  27. // ...
  28. }
英文:

As one of the possible approaches you can use the following mapping.

Assuming that you have the following database schema:

  1. create table TST_MATCH
  2. (
  3. mt_id int not null,
  4. primary key (mt_id)
  5. );
  6. create table TST_COUPON
  7. (
  8. cn_id int not null,
  9. primary key (cn_id)
  10. );
  11. create table TST_MATCH_COUPON
  12. (
  13. match_id int not null,
  14. coupon_id int not null,
  15. UNIQUE (match_id, coupon_id),
  16. foreign key (match_id) references TST_MATCH(mt_id),
  17. foreign key (coupon_id) references TST_COUPON(cn_id)
  18. );

Your mapping can be like this:

  1. @Entity
  2. @Table(name = &quot;TST_MATCH&quot;)
  3. public class Match
  4. {
  5. @Id
  6. @Column(name = &quot;mt_id&quot;)
  7. private Integer id;
  8. @ManyToMany(mappedBy = &quot;matchs&quot;, cascade = CascadeType.ALL)
  9. private List&lt;Coupon&gt; coupons;
  10. // ...
  11. }
  12. @Entity
  13. @Table(name = &quot;TST_COUPON&quot;)
  14. public class Coupon
  15. {
  16. @Id
  17. @Column(name = &quot;cn_id&quot;)
  18. private Integer id;
  19. @ManyToMany(cascade = CascadeType.ALL)
  20. @JoinTable(
  21. name = &quot;TST_MATCH_COUPON&quot;,
  22. inverseJoinColumns = { @JoinColumn(name = &quot;match_id&quot;, referencedColumnName = &quot;mt_id&quot;) },
  23. joinColumns = { @JoinColumn(name = &quot;coupon_id&quot;, referencedColumnName = &quot;cn_id&quot;) }
  24. )
  25. @MapKey(name = &quot;id&quot;)
  26. private Map&lt;Integer, Match&gt; matchs;
  27. // ...
  28. }

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:

确定