Skip to content

Instantly share code, notes, and snippets.

@MikhailEpatko
Created August 15, 2019 19:07
@Immutable
@Entity
@Table(name = "post_tag")
@Getter
@EqualsAndHashCode
@NoArgsConstructor
public class PostTag {
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
@Embeddable
public static class Id implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "post_id")
protected UUID postId;
@Column(name = "tag_id")
protected UUID tagId;
}
@EmbeddedId
protected PostTag.Id id = new PostTag.Id();
@ManyToOne
@JoinColumn(name = "post_id", insertable = false, updatable = false)
private Post post;
@ManyToOne
@JoinColumn(name = "tag_id", insertable = false, updatable = false)
private Tag tag;
public PostTag(Post post, Tag tag) {
this.post = post;
this.tag = tag;
this.id.postId = post.getId();
this.id.tagId = tag.getId();
post.getPostTags().add(this);
tag.getPostTags().add(this);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment