title: Why I get Object references an unsaved transient instance
body: <p>I have many-to-many relationship between two entities: Tag and Ads. When User wants to create ad, he need to insert few tags. But when I try to create add I am getting this error:</p>
  <pre><code>{
      &quot;timestamp&quot;: &quot;2020-10-11T21:21:44.752+0000&quot;,
      &quot;status&quot;: 500,
      &quot;error&quot;: &quot;Internal Server Error&quot;,
      &quot;message&quot;: &quot;org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.mybrocki.entity.Tag; nested exception is java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.mybrocki.entity.Tag&quot;,
      &quot;path&quot;: &quot;/mybrocki/auth/ads/create&quot;
  }
  </code></pre>
  <p>This is my Tag entity:</p>
  <pre><code>@Entity
  @Table(name=&quot;Tags&quot;)
  public class Tag {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
      
      @Column(name=&quot;name&quot;)
      private String name;
  
      @ManyToMany(mappedBy = &quot;adsTags&quot;,  cascade = {
              CascadeType.ALL
          })
      private Set&lt;Ads&gt; ads;
      
      
      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&lt;Ads&gt; getAds() {
          return ads;
      }
  
      public void setAds(Set&lt;Ads&gt; ads) {
          this.ads = ads;
      }
  </code></pre>
  <p>And this is Ads entity:</p>
  <pre><code>@ManyToMany
      @JoinTable(name = &quot;tags_ads&quot;,
          joinColumns = @JoinColumn(name = &quot;ads_id&quot;, referencedColumnName = &quot;id&quot;),
          inverseJoinColumns = @JoinColumn(name = &quot;tag_id&quot;, referencedColumnName = &quot;id&quot;))
      private List&lt;Tag&gt; adsTags;
  </code></pre>
  <p>And this is service implementation class:</p>
  <pre><code>ads.setVisibleAds(false);
                      ads.setAdsTags(new ArrayList&lt;Tag&gt;());
                      if (!adsDTO.getTags().isEmpty()) {
                          for (Tag tagDTO : adsDTO.getTags()) {
                      ads.getAdsTags().add(tagDTO); }
                      
  
                      }
                      ads.setStatus(AdsStatus.READYFORREVIEW.toString());
                      ads.setAdssubgroup(adsSubGroupRepository.findOneById(adsDTO.getAdsSubGroupId()));
                      ads.setFixedPrice(adsDTO.getFixedPrice());
                      ads.setFreeDelivery(adsDTO.getFreeDelivery());
                      ads.setProductWarranty(adsDTO.getProductWarranty());
                      ads.setUrgentSales(adsDTO.getUrgentSales());
                      Date currentDate = new Date();
                      Instant instant = currentDate.toInstant();
                      LocalDateTime ldt = instant.atOffset(ZoneOffset.UTC).toLocalDateTime();
                      DateTimeFormatter formatter = DateTimeFormatter.ofPattern(&quot;yyyy-MM-dd&quot;);
                      
                      adsDTO.setAdsDate(currentDate);
                      ads.setAdsDate(adsDTO.getAdsDate());
                      
                      adsRepository.save(ads);
              
  </code></pre>
  <p>This is json:</p>
  <pre><code>{
    &quot;adsGroupId&quot;: 5,
    &quot;adsSubGroupId&quot;: 27,
    &quot;adsType&quot;: &quot;USED&quot;,
    &quot;description&quot;: &quot;Ford fiesta 1.3&quot;,
    &quot;fixedPrice&quot;: false,
    &quot;freeDelivery&quot;: false,
    &quot;image&quot;: [
      &quot;http://res.cloudinary.com/mybrocki/image/upload/v1589887253/s5d11rshydka5nov2wox.jpg,http://res.cloudinary.com/mybrocki/image/upload/v1589887253/s5d11rshydka5nov2wox.jpg,&quot;
    ],
    &quot;price&quot;: 800,
    &quot;productName&quot;: &quot;FOcscscsdcRD Goragjhggiujkjhkjoijiohiuhn&quot;,
    &quot;productWarranty&quot;: false,
    &quot;urgentSales&quot;: true,
      &quot;tags&quot;: [
          {
          &quot;name&quot;:&quot;Second hand&quot;
      }]
  }
  </code></pre>
  <p>What could be the problem?</p>