在连接表中的复合ID

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

Composite ID in join-table

问题

我有以下的PostLike类:

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PostLike extends BaseEntity {

    @ManyToOne
    @JoinColumn(name = "user_id")
    private User user;

    @ManyToOne
    @JoinColumn(name = "post_id")
    private Post post;
}

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

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

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

而在Post类中:

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

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

英文:

I have the following PostLike class:

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PostLike extends BaseEntity {

    @ManyToOne
    @JoinColumn(name = &quot;user_id&quot;)
    private User user;

    @ManyToOne
    @JoinColumn(name = &quot;post_id&quot;)
    private Post post;
}

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

In the User class, I have the following field:

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

And in the Post class:

@OneToMany(mappedBy = &quot;post&quot;)
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注解,并使用嵌入式类表示这个复合键:

@Entity
public class PostLike {

    @Embeddable
    private static class Id implements Serializable {
        
        @Column(name = "user_id")
        private Long userId;
        
        @Column(name = "post_id") 
        private Long postId;

        public Id() {
        }

        public Id(Long userId, Long postId) {
            this.userId = userId;
            this.postId = postId;
        }

        // equals 和 hashCode 方法
    }

    @EmbeddedId
    Id id = new Id();

    @ManyToOne
    @JoinColumn(name = "user_id")
    private User user;

    @ManyToOne
    @JoinColumn(name = "post_id")
    private Post post;


    public PostLike() {
    }

    public PostLike(User user , Post post) {
        this.user = user;
        this.post = post;

        this.id.postId = post.getId();
        this.id.userId = user.getId();

        post.getUserLikes().add(this);
        user.getUserLikes().add(this);
    }
        ... // 获取器和设置器
}

一些注意事项:
来自@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:

@Entity
public class PostLike {

    @Embeddable
    private static class Id implements Serializable {
        
        @Column(name = &quot;user_id&quot;)
        private Long userId;
        
        @Column(name = &quot;post_id&quot;) 
        private Long postId;

        public Id() {
        }

        public Id(Long userId, Long postId) {
            this.userId = userId;
            this.postId = postId;
        }

        // equals and hashCode
    }

    @EmbeddedId
    Id id = new Id();

    @ManyToOne
    @JoinColumn(name = &quot;user_id&quot;)
    private User user;

    @ManyToOne
    @JoinColumn(name = &quot;post_id&quot;)
    private Post post;


    public PostLike() {
    }

    public PostLike(User user , Post post) {
        this.user = user;
        this.post = post;

        this.id.postId = post.getId();
        this.id.userId = user.getId();

        post.getUserLikes().add(this);
        user.getUserLikes().add(this);
    }
        ... // getters and setters
}

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://go.coder-hub.com/61018594.html
匿名

发表评论

匿名网友

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

确定