在连接表中的复合ID

huangapple 未分类评论49阅读模式
英文:

Composite ID in join-table

问题

我有以下的PostLike类:

  1. @Entity
  2. @Data
  3. @NoArgsConstructor
  4. @AllArgsConstructor
  5. public class PostLike extends BaseEntity {
  6. @ManyToOne
  7. @JoinColumn(name = "user_id")
  8. private User user;
  9. @ManyToOne
  10. @JoinColumn(name = "post_id")
  11. private Post post;
  12. }

该类已经由父类BaseEntity提供了一个ID字段。

在User类中,我有以下字段:

  1. @OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
  2. Set<PostLike> userLikes = new HashSet<PostLike>();

而在Post类中:

  1. @OneToMany(mappedBy = "post")
  2. private Set<PostLike> postLikes = new HashSet<PostLike>();

我希望PostLike拥有一个由user_id和post_id组成的复合主键。提前感谢您的帮助。

英文:

I have the following PostLike class:

  1. @Entity
  2. @Data
  3. @NoArgsConstructor
  4. @AllArgsConstructor
  5. public class PostLike extends BaseEntity {
  6. @ManyToOne
  7. @JoinColumn(name = &quot;user_id&quot;)
  8. private User user;
  9. @ManyToOne
  10. @JoinColumn(name = &quot;post_id&quot;)
  11. private Post post;
  12. }

The class already has an ID field provided by parent BaseEntity class.

In the User class, I have the following field:

  1. @OneToMany(mappedBy = &quot;user&quot;, fetch = FetchType.LAZY)
  2. Set&lt;PostLike&gt; userLikes = new HashSet&lt;PostLike&gt;();

And in the Post class:

  1. @OneToMany(mappedBy = &quot;post&quot;)
  2. private Set&lt;PostLike&gt; postLikes = new HashSet&lt;PostLike&gt;();

I want a PostLike to have a composite primary key, which consists of user_id and post_id.
Thanks in advance.

答案1

得分: 1

作为实现这一目标的一种方法,您可以使用@EmbeddedId注解,并使用嵌入式类表示这个复合键:

  1. @Entity
  2. public class PostLike {
  3. @Embeddable
  4. private static class Id implements Serializable {
  5. @Column(name = "user_id")
  6. private Long userId;
  7. @Column(name = "post_id")
  8. private Long postId;
  9. public Id() {
  10. }
  11. public Id(Long userId, Long postId) {
  12. this.userId = userId;
  13. this.postId = postId;
  14. }
  15. // equals 和 hashCode 方法
  16. }
  17. @EmbeddedId
  18. Id id = new Id();
  19. @ManyToOne
  20. @JoinColumn(name = "user_id")
  21. private User user;
  22. @ManyToOne
  23. @JoinColumn(name = "post_id")
  24. private Post post;
  25. public PostLike() {
  26. }
  27. public PostLike(User user , Post post) {
  28. this.user = user;
  29. this.post = post;
  30. this.id.postId = post.getId();
  31. this.id.userId = user.getId();
  32. post.getUserLikes().add(this);
  33. user.getUserLikes().add(this);
  34. }
  35. ... // 获取器和设置器
  36. }

一些注意事项:
来自@EmbeddedId的Java文档:
> 当使用@EmbeddedId注解时,不能有多个Id注解。

来自《Java Persistence with Hibernate》
> 这种策略的主要优势是可以进行双向导航。
...
> 不利之处在于需要更复杂的代码来管理中间实体实例以创建和删除链接,您需要单独保存和删除这些实例。

英文:

As one way to do this you can use @EmbeddedId annotation and express this composite key with embeddable class:

  1. @Entity
  2. public class PostLike {
  3. @Embeddable
  4. private static class Id implements Serializable {
  5. @Column(name = &quot;user_id&quot;)
  6. private Long userId;
  7. @Column(name = &quot;post_id&quot;)
  8. private Long postId;
  9. public Id() {
  10. }
  11. public Id(Long userId, Long postId) {
  12. this.userId = userId;
  13. this.postId = postId;
  14. }
  15. // equals and hashCode
  16. }
  17. @EmbeddedId
  18. Id id = new Id();
  19. @ManyToOne
  20. @JoinColumn(name = &quot;user_id&quot;)
  21. private User user;
  22. @ManyToOne
  23. @JoinColumn(name = &quot;post_id&quot;)
  24. private Post post;
  25. public PostLike() {
  26. }
  27. public PostLike(User user , Post post) {
  28. this.user = user;
  29. this.post = post;
  30. this.id.postId = post.getId();
  31. this.id.userId = user.getId();
  32. post.getUserLikes().add(this);
  33. user.getUserLikes().add(this);
  34. }
  35. ... // getters and setters
  36. }

Some notes:
from javadoc of @EmbeddedId
> There must be only one <code>EmbeddedId</code> annotation and
no <code>Id</code> annotation when the <code>EmbeddedId</code> annotation is used.

from Java Persistence with Hibernate
> The primary advantage of this strategy is the possibility for bidirectional navigation.
...
> A disadvantage is the more complex code needed to manage the
intermediate entity instances to create and remove links, which you have to save
and delete independently.

huangapple
  • 本文由 发表于 2020年4月4日 02:49:09
  • 转载请务必保留本文链接:https://java.coder-hub.com/61018594.html
匿名

发表评论

匿名网友

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

确定