Announcement Announcement Module
No announcement yet.
Question on Repository's entitymanager's scope in Srping Data JPA. Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Question on Repository's entitymanager's scope in Srping Data JPA.

    I'm trying to make Spring JPA Data work for me, but have been struggling. Here is the problem.

    I have two domain classes with a simple OneToMany relation between them:
    class Card{
    private User user;
    class User(){
    private List<Card> cards;
    I have set up Repository interface for each of the class: CardRepository, UserRepository extending the JpaRepository, both repository is injected into a service

    @Transactional(readOnly = true)
    class Service{
      CardRepository repo1;
      UserRepository repo2;
      public void someMethod(){
          // make use of the repos
          User u=repo2.findByIdentifier("ID1");
          List<Card> cards = u.getCards();
          //do something with the cards will throw lazyinitialization exception.
    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.

    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?

    If I however set the @OneToMany's fetch to be eager, no problem, but it is something I really don't want to do.

    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!

    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


  • #2
    Your description of the symptoms you have lead me to believe you have a wrongly configured transactional setup. Looks like your service isn't transactional. You use @Service and I guess also component-scanning, make sure you aren't scanning for the same components twice which leads to duplication of your services and in general 1 proxied and 1 unproxied instance.

    With regard to the delete there is no delete going to happen but probably an update to remove the link from the user to the card but as you already stated the card isn't there. Imho nothing related to Spring Data but merely normal behavior.


    • #3
      thanks for the reply.

      that was my suspicion as well. I don't have any transaction setting up except in the spring configuration:

      	<bean id="entityManagerFactory"
      		<property name="dataSource" ref="dataSource" />
      		<property name="packagesToScan" value="com.dongluhitec.card.domain" />
      		<property name="jpaVendorAdapter">
      			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
      				<property name="showSql" value="true" />
      				<property name="generateDdl" value="true" />
      				<property name="database" value="DERBY" />
      				<property name="databasePlatform" value="org.hibernate.dialect.DerbyTenSevenDialect" />
      	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
      		<property name="entityManagerFactory" ref="entityManagerFactory" />
      Is this setting correct to use for JPA Data ?


      • #4
        If you don't have a tx:annotation-driven there is nothing that drives the transaction. Also as mentioned before make sure you aren't scanning for the same components twice.