title: Need solution for following scenario in Hibernate many to many mapping
body: <p>Consider the tables where <strong>posts</strong> and <strong>tags</strong> exhibit a many-to-many relationship between each other.</p>
  
  <p>The many-to-many relationship is implemented using a third table called post_tags which contains the details of posts and their associated tags.</p>
  
  <pre><code>Post Model
  
  @Entity
  @Table(name = "posts")
  public class Post {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      @NotNull
      @Size(max = 100)
      @Column(unique = true)
      private String title;
  
      @NotNull
      @Size(max = 250)
      private String description;
  
      @NotNull
      @Lob
      private String content;
  
      @NotNull
      @Temporal(TemporalType.TIMESTAMP)
      @Column(name = "posted_at")
      private Date postedAt = new Date();
  
      @NotNull
      @Temporal(TemporalType.TIMESTAMP)
      @Column(name = "last_updated_at")
      private Date lastUpdatedAt = new Date();
  
  
      @ManyToMany(fetch = FetchType.LAZY,
              cascade = {
                  CascadeType.PERSIST,
                  CascadeType.MERGE
              })
      @JoinTable(name = "post_tags",
              joinColumns = { @JoinColumn(name = "post_id") },
              inverseJoinColumns = { @JoinColumn(name = "tag_id") })
      private Set&lt;Tag&gt; tags = new HashSet&lt;&gt;();
  
  
      public Post() {
  
      }
  
      public Post(String title, String description, String content) {
          this.title = title;
          this.description = description;
          this.content = content;
      }
  
      // Getters and Setters (Omitted for brevity)
  }
  </code></pre>
  
  <hr>
  
  <pre><code>TAG Model
  
  @Entity
  @Table(name = "tags")
  public class Tag {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      @NotNull
      @Size(max = 100)
      @NaturalId
      private String name;
  
      @ManyToMany(fetch = FetchType.LAZY,
              cascade = {
                  CascadeType.PERSIST,
                  CascadeType.MERGE
              },
              mappedBy = "tags")
      private Set&lt;Post&gt; posts = new HashSet&lt;&gt;();
  
      public Tag() {
  
      }
  
      public Tag(String name) {
          this.name = name;
      }
  
      // Getters and Setters (Omitted for brevity)
  }
  </code></pre>
  
  <p>Problem is</p>
  
  <p>I tried to use an existing tags. and insert happened only on posts &amp; posts_tags table.</p>
  
  <p>Initially i'm Get tag(s) with tagName(s). Once you have the Tag object, you can set it in the Post object and save it.</p>
  
  <p>Like this</p>
  
  <pre><code> Post post = new Post("Hibernate Many to Many Example with Spring Boot",
                  "Learn how to map a many to many relationship using hibernate",
                  "Entire Post content with Sample code");
  
          // Create two tags
          Tag tag1 = tagService.getTag("Spring Boot");
  
  
          // Add tag references in the post
          post.getTags().add(tag1);
  
          postRepository.save(post);
  </code></pre>
  
  <p><strong>If I do like that, entry is not available in post_tags table.</strong></p>
  
  <p>Tag Repository and Tag Service:</p>
  
  <pre><code>@Repository
  public interface TagRepository extends JpaRepository&lt;Tag, Long&gt; {
  
      @Query("select p from Tag p where p.name = :name")
      Tag findByName(@Param("name") String name);
  }
  
  @Override
  public Tag findByName(String name) {
    return repository.findByName(name);
  }
  </code></pre>