Skip to content

Instantly share code, notes, and snippets.

@Tonel
Created July 4, 2020 10:40
Show Gist options
  • Save Tonel/066d27a0516310395bcf4dea4c57e093 to your computer and use it in GitHub Desktop.
Save Tonel/066d27a0516310395bcf4dea4c57e093 to your computer and use it in GitHub Desktop.
@Entity
@Table(name = "post")
public class Post {
@Id
@GeneratedValue
@Column(name = "id")
private Long id;
@Column(name = "title")
private String title;
@ManyToMany
@JoinTable(name = "post_tag",
joinColumns = @JoinColumn(name = "post_id"),
inverseJoinColumns = @JoinColumn(name = "tag_id")
)
private Set<Tag> tags = new HashSet<>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Set<Tag> getTags() {
return tags;
}
public void setTags(Set<Tag> tags) {
this.tags = tags;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Post tag = (Post) o;
return Objects.equals(id, tag.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}
@Entity
@Table(name = "tag")
public class Tag {
@Id
@GeneratedValue
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
@ManyToMany(mappedBy = "tags")
private Set<Post> posts = new HashSet<>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Post> getPosts() {
return posts;
}
public void setPosts(Set<Post> posts) {
this.posts = posts;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Tag tag = (Tag) o;
return Objects.equals(id, tag.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment