英文:
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 = "user_id")
private User user;
@ManyToOne
@JoinColumn(name = "post_id")
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 = "user", fetch = FetchType.LAZY)
Set<PostLike> userLikes = new HashSet<PostLike>();
And in the Post class:
@OneToMany(mappedBy = "post")
private Set<PostLike> postLikes = new HashSet<PostLike>();
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 = "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 and 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);
}
... // 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论