title: Spring JPA Data's Repository
body: <p>I'm trying to make Spring JPA Data work for me, but have been struggling. Here is the problem. </p>
  
  <p>I have two domain classes with a simple OneToMany relation between them:</p>
  
  <pre><code>class Card {
      @ManyToOne(mappedBy="user")
      private User user;
  }
  
  class User {
       @OneToMany
       private List&lt;Card&gt; cards;
  }
  </code></pre>
  
  <p>I have set up Repository interface for each of the class: CardRepository, UserRepository extending the JpaRepository, both repository is injected into a service</p>
  
  <pre><code>@Service
  @Transactional(readOnly = true)
  class Service {
    @Autowired
    CardRepository repo1;
    @Autowired
    UserRepository repo2;
  
    public void someMethod() {
        // make use of the repos
        User u=repo2.findByIdentifier("ID1");
        List&lt;Card&gt; cards = u.getCards();
        //do something with the cards will throw lazyinitialization exception.
    }
  }
  </code></pre>
  
  <p>pretty basic setting up.
  problem comes with the someMethod(), in which I queried an User with its identifier, then try to get the mapped @OneToMany's list, then the LazyInitialization exception happened.</p>
  
  <p>I'm not quite sure if I missed something there? seems as long as the repository's method is returned, the entitymanager is closed; If that's the case, i'm wondering how can I get the relationship without define another repository method? </p>
  
  <p>If I however set the @OneToMany's fetch to be eager, no problem, but it is something I really don't want to do.</p>
  
  <p>The delete() of the repository seems also problematic. If I delete a card first, then trying to delete() its owning user (which still have the card in its list), the delete will fail complaining cannot find the card. but I didn't set any removal propagation from the User to the Card!</p>
  
  <p>I hope someone can explain how the entitymanager is used in the JpaRepository, it seems making Jpa programming more harder. I know all the repository is automatically generated but if someone can point to how they are implemented that wil be very helpful.  </p>
  
  <p>Thanks.
  Wudong</p>