title: Spring Jpa Data Repository save (update) with LinkedEntity for ManyToMany relationship
body: <p>There are 2 entities (lets say Rule and Label) with many-to-many relationship using linked entity
  <a href="http://docs.jboss.org/hibernate/orm/5.3/userguide/html_single/Hibernate_User_Guide.html#associations-many-to-many-bidirectional-with-link-entity." rel="nofollow noreferrer">as per hibernate reference documentation</a>
  <br>
  <strong>Rule enity:</strong></p>
  
  <pre><code>@Entity
  @Table(name = "rule")
  @JsonIdentityInfo(
      generator = ObjectIdGenerators.PropertyGenerator.class,
      property = "name")
  public class Rule implements Serializable {
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
  
  @NaturalId
  @NotBlank
  @Column(unique = true)
  private String name;
  
  @Lob
  @Column(columnDefinition = "TEXT")
  private String content;
  
  @OneToMany(mappedBy = "rule", cascade = {CascadeType.PERSIST, 
      CascadeType.MERGE})
  private List&lt;RuleLabel&gt; labels = new ArrayList&lt;&gt;();
  ...
  </code></pre>
  
  <p><br>
  <strong>Label entity:</strong></p>
  
  <pre><code>@Entity
  @Table(name = "label")
  @JsonIdentityInfo(
      generator = ObjectIdGenerators.PropertyGenerator.class,
      property = "id")
  public class Label implements Serializable {
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
  
  @NotBlank
  private String name;
  
  @OneToMany(mappedBy = "label", cascade = {CascadeType.PERSIST, 
      CascadeType.MERGE})
  private List&lt;RuleLabel&gt; rules = new ArrayList&lt;&gt;();
  ...
  </code></pre>
  
  <p><br>
  <strong>Link entity:</strong></p>
  
  <pre><code>@Entity
  public class RuleLabel implements Serializable {
  
  @Id
  @ManyToOne
  private Rule rule;
  
  @Id
  @ManyToOne
  private Label label;
  ...
  </code></pre>
  
  <p><br>
  <strong>Repositories:</strong></p>
  
  <pre><code>@Repository
  public interface LabelRepository extends JpaRepository&lt;Label, Long&gt;
  ...
  @Repository
  public interface RuleRepository extends JpaRepository&lt;Rule, Long&gt;
  ...
  </code></pre>
  
  <p><br>
  Creating new entity via <strong>RuleRepository.save(Rule)</strong> works fine, but when I'm trying to update existing entity (the same method <strong>RuleRepository.save(Rule)</strong>, but entity to be saved contains <strong>id</strong> field) it leads to infinite loop of <strong>Hibernate: select...</strong> queries:</p>
  
  <pre><code>Hibernate: select rule0_.id as id1_7_1_, rule0_.is_active as is_activ2_7_1_, rule0_.content as content3_7_1_, rule0_.is_deleted as is_delet4_7_1_, rule0_.import_section as import_s5_7_1_, rule0_.name as name6_7_1_, rule0_.rule_configuration as rule_con7_7_1_, labels1_.rule_id as rule_id1_8_3_, labels1_.label_id as label_id2_8_3_, labels1_.rule_id as rule_id1_8_0_, labels1_.label_id as label_id2_8_0_ from rule rule0_ left outer join rule_label labels1_ on rule0_.id=labels1_.rule_id where rule0_.id=?
  </code></pre>
  
  <p>and StackOverflowError as a result </p>
  
  <pre><code>java.lang.StackOverflowError: null
  at com.mysql.jdbc.ServerPreparedStatement.getInstance(ServerPreparedStatement.java:332)
  ...
  </code></pre>
  
  <p>(LabelRepository acts in the same manner)
  <br>
  How it can be fixed?
  <br>
  <strong>Update:</strong>
  After changing fetch strategy to Lazy</p>
  
  <pre><code>@Id
  @ManyToOne(fetch = FetchType.LAZY)
  private Rule rule;
  
  @Id
  @ManyToOne(fetch = FetchType.LAZY)
  private Label label;
  </code></pre>
  
  <p>infinite loop problem has gone, but new one has appeared - related entities are not being populated and when Hibernate is trying to insert values into link table</p>
  
  <pre><code>Hibernate: insert into rule_label (rule_id, label_id) values (?, ?)
  </code></pre>
  
  <p>we get</p>
  
  <pre><code>org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
  ...
  Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'rule_id' cannot be null
  </code></pre>