Relação SpringTo-Data-JPA ManyToMany com coluna extra
Estou lutando para estabelecer um relacionamento de muitos para muitos com uma coluna adicional na tabela de link
Estas são minhas entidades:
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
public class Post {
@Id
@GeneratedValue
private Long id;
private String name;
@OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonIgnore
private List<PostTag> tags = new ArrayList<>();
//getters and setters
public void addTag(Tag tag){
PostTag postTag = new PostTag(this, tag);
tags.add(postTag);
tag.getPosts().add(postTag);
}
public void removeTag(Tag tag) {
for (Iterator<PostTag> iterator = tags.iterator();
iterator.hasNext(); ) {
PostTag postTag = iterator.next();
if (postTag.getPost().equals(this) &&
postTag.getTag().equals(tag)) {
iterator.remove();
postTag.getTag().getPosts().remove(postTag);
postTag.setPost(null);
postTag.setTag(null);
}
}
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Post post = (Post) o;
return id == post.id;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
public class Tag {
@Id
@GeneratedValue
private Long id;
private String comment;
@OneToMany(mappedBy = "tag", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonIgnore
private List<PostTag> posts = new ArrayList<>();
//getters and setters
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Tag that = (Tag) o;
return id == that.id;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}
@Entity(name = "PostTag")
@Table(name = "post_tag")
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
public class PostTag {
@EmbeddedId
private PostTagId id;
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("postId")
private Post post;
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("tagId")
private Tag tag;
private Integer impact;
public FacilityParticipant(Post post, Tag tag) {
this.post = post;
this.tag = tag;
this.id = new PostTagId(post.getId(), tag.getId());
}
//getters and setters
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
PostTag that = (PostTag) o;
return Objects.equals(post, that.post) && Objects.equals(tag, that.tag);
}
@Override
public int hashCode() {
return Objects.hash(post, tag);
}
}
@Embeddable
public class PostTagId implements Serializable {
private Long postId;
private Long tagId;
//getters setters
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
PostTagId that = (PostTagId) o;
return Objects.equals(postId, that.postId) && Objects.equals(tagId, that.tagId);
}
@Override
public int hashCode() {
return Objects.hash(postId, tagId);
}
}
Tenho uma entidade de postagem mapeada para muitas tags e uma tag que está mapeada para muitas postagens. A tabela de links é PostTag, que contém os mapeamentos para os dois lados e a coluna adicional "impact". A PK da tabela de links é mapeada para uma tabela incorporável PostTagId, que contém a PK de Post e Tag.
Quando tento inserir novas entidades, faço o seguinte:
Tag tag1 = new Tag();
Tag tag2 = new Tag();
repository.save(tag1);
repository.save(tag2);
Post post1 = new Post();
Post post2 = new Post();
post1.addTag(tag1);
post1.addTag(tag2);
post2.addTag(tag1);
repository.save(post1);
repository.save(post2);
Ao tentar inserir esses itens, recebo o erro de que não é possível inserir NULL em ("POST_TAG". "ID")
Qualquer coisa que eu tentei, ou vem com outros erros ou fica de volt
Provavelmente algo do modelo não está certo, mas realmente não consigo entender o que há de errado com el
Toda a modelagem foi baseada neste artigoO melhor caminho para ..
Qualquer ajuda seria muito apreciada
Obrigad