Reputation: 1787
I have a many to many relationship between POSTs and TAGs. Each post can have many tags, each tag can have many posts.
I would like to manage the relationship with an entity mapped to the join table. So I have 3 entity classes: Post
, Tag
, PostTag
. PostTag
has @ManyToOne
annotated Post
and Tag
members.
The PostTag
and it's embedded key:
@Entity
@Table(name = "post_tag")
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 PostTag() {}
public PostTag(Post post, Tag tag) {
this.post = post;
this.tag = tag;
this.id = new PostTagId(post.getId(), tag.getId());
}
public PostTagId getId() { return id; }
public void setId(PostTagId id) { this.id = id; }
public Post getPost() { return post; }
public void setPost(Post post) { this.post = post; }
public Tag getTag() { return tag; }
public void setTag(Tag tag) { this.tag = tag; }
}
@Embeddable
public class PostTagId implements Serializable {
@Column(name = "post_id")
private Long postId;
@Column(name = "tag_id")
private Long tagId;
public PostTagId() {}
public PostTagId(Long postId, Long tagId) {
this.postId = postId;
this.tagId = tagId;
}
public Long getPostId() { return postId; }
public void setPostId(Long postId) { this.postId = postId; }
public Long getTagId() { return tagId; }
public void setTagId(Long tagId) { this.tagId = tagId; }
}
When I need the Posts with a given Tag, I would like to use this query:
@Query(value = "select pt from PostTag pt join fetch pt.post where pt.id.tagId = :tagId")
Set<PostTag> findByTagIdAndFetchPosts(@Param("tagId") Long tagId);
The problem is that Hibernate creates this select:
2020-08-25 10:21:57.486 DEBUG 16791 --- [ main] org.hibernate.SQL :
select
posttag0_.post_id as post_id1_1_0_,
posttag0_.tag_tag_id as tag_tag_2_1_0_
from
post_tag posttag0_
where
posttag0_.post_id=?
and posttag0_.tag_tag_id=?
Which causes the following error:
2020-08-25 10:21:57.487 WARN 16791 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 42122, SQLState: 42S22
2020-08-25 10:21:57.487 ERROR 16791 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : Column "POSTTAG0_.TAG_TAG_ID" not found; SQL statement:
select posttag0_.post_id as post_id1_1_0_, posttag0_.tag_tag_id as tag_tag_2_1_0_ from post_tag posttag0_ where posttag0_.post_id=? and posttag0_.tag_tag_id=? [42122-200]
What's wrong with it?
The whole thing is here:
https://github.com/riskop/jpa_hibernate_spring_boot_many_to_many_managed_on_join_table_problem
Upvotes: 1
Views: 241
Reputation: 13041
When you use @MapsId
hibernate actually ignores the column name provided in the @Column
annotation of the appropriate field of the @Embeddable
class and starts to use the name from the @JoinColumn
. If the @JoinColumn
is absent the default conventions are applied (the concatenation of the name of the referencing relationship property; "_"; the name of the referenced primary key column).
You have:
@Entity
public class Tag {
@Id
@Column(name="TAG_ID")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
// ...
}
@Entity
@Table(name = "post_tag")
public class PostTag {
@EmbeddedId
private PostTagId id;
// There is no @JoinColumn annotation !!!
// So, the default naming convention is used
// "tag" + "_" + "tag_id"
// "tag_id" is the Tag's entity PK column name
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("tagId")
private Tag tag;
// ...
}
So, you can fix your mapping in this way:
@Entity
@Table(name = "post_tag")
public class PostTag {
@EmbeddedId
private PostTagId id;
// by chance the default naming convention
// lead to the same column name for this case
@MapsId("postId")
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id")
private Post post;
@MapsId("tagId")
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "tag_id")
private Tag tag;
// ...
}
and you can remove the @Column
annotations from the PostTagId
fields:
@Embeddable
public class PostTagId implements Serializable {
private Long postId;
private Long tagId;
// ...
}
as they are ignored.
Upvotes: 1