title: Spring Boot JPA - OneToMany relationship causes infinite loop
body: <p>I have a two objects with simple @OneToMany relationship which looks as follows:</p>
  
  <p>parent:</p>
  
  <pre><code>@Entity
  public class ParentAccount {
  
    @Id
    @GeneratedValue
    private long id;
    private String name;
  
    @OneToMany(fetch = FetchType.EAGER, mappedBy = "parentAccount")
    private Set&lt;LinkedAccount&gt; linkedAccounts;
  
  }
  </code></pre>
  
  <p>child:</p>
  
  <pre><code>@Entity
  public class LinkedAccount {
  
    @Id
    @GeneratedValue
    private long id;
  
    @ManyToOne(optional = false)
    private ParentAccount parentAccount;
  
    private String name;
  
    // empty constructor for JPA
    public LinkedAccount() {
    }
  
  }
  </code></pre>
  
  <p>I ma using Spring <code>CrudRepository</code> to operate with these entities. However, when calling <code>ParentAccount parent = parentAccountRepository.findOne(id);</code>, some kind of infinite loop starts happening and hibernate spams this all over the console:</p>
  
  <pre><code>Hibernate: select linkedacco0_.parent_account_id as parent_a6_1_0_, linkedacco0_.id as id1_0_0_, linkedacco0_.id as id1_0_1_, linkedacco0_.aws_id as aws_id2_0_1_, linkedacco0_.key_id as key_id3_0_1_, linkedacco0_.name as name4_0_1_, linkedacco0_.parent_account_id as parent_a6_0_1_, linkedacco0_.secret_key as secret_k5_0_1_ from linked_account linkedacco0_ where linkedacco0_.parent_account_id=?
  </code></pre>
  
  <p>I tried changed the fetch type to LAZY but then I get this error:</p>
  
  <pre><code>org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.berrycloud.scheduler.model.ParentAccount.linkedAccounts, could not initialize proxy - no Session
  </code></pre>
  
  <p>(It seems that it is trying to do the lazy load outside of the transactional context).</p>
  
  <p>This is my CRUD repository:</p>
  
  <pre><code>@Repository
  public interface ParentAccountRepository extends CrudRepository&lt;ParentAccount, Long&gt; {
  }
  </code></pre>
  
  <p>Could someone tell me how to resolve this issue? I would prefer the solution with EAGER fetch. Thank you for any tips</p>
  
  <p>EDIT: here is the schema I am using</p>
  
  <pre><code>CREATE TABLE parent_account (
      id BIGINT auto_increment,
      name VARCHAR(80) null,
      PRIMARY KEY (`id`)
  );
  
  CREATE TABLE linked_account (
      id BIGINT auto_increment,
      parent_account_id BIGINT,
      name VARCHAR(80) null,
      FOREIGN KEY (`parent_account_id`) REFERENCES `parent_account` (`id`),
      PRIMARY KEY (`id`)
  );
  </code></pre>