Announcement Announcement Module
Collapse
No announcement yet.
One-To-One relation with lazy fetch Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • One-To-One relation with lazy fetch

    In our java application we have two entities: A main account and the settings for this account. We use hibernate for providing persistence. We want the account settings to be lazy loaded. So we did this:

    AccountMain:
    Code:
    @OneToOne(optional = false, cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
    private AccountMainSettings        accountMainSettings;
    
    @JoinColumn(name = AccountMainSettings.ACCOUNT_MAIN_SETTINGS_ID, unique = true, nullable = false, updatable = false, insertable = true)
    public final AccountMainSettings getAccountMainSettings() {
    return this.accountMainSettings;
    }
    AccountMainSettings:
    Code:
    @OneToOne(mappedBy = "accountMainSettings")
    private AccountMain        accountMain;
    
    public final AccountMain getAccountMain() {
    return this.accountMain;
    }
    When we load the AccountMainSettings object in AccountMain it is proxied as it should be. But when we call a method of AccountMainSettings, the object is not loaded from the database an an NPE is thown of course. Having read Making-a-onetoone-relation-lazy didn't help much. We neither have a nullable association nor do we want to convert it to a ManyToOne association. When we switch to eager loading the problem is "solved", as the settings are loaded but they contain many fields so we don't want them to be unnecessarily loaded.

    How can we implement lazy loading in this context?

    Here our jUnit Test:
    Code:
    @Test
    public final void getMainAccountByAccountId() {
    final AccountMain accountMain = this.accountMainDAO.getMainAccountByAccountId(PersistTestCaseConstants.SAVED_MAIN_ACCOUNT_ID);
    final AccountMainSettings accountMainSettings = accountMain.getAccountMainSettings();
    final String imprint = accountMainSettings.getImprint();
    assertEquals(PersistTestCaseConstants.OBJECT_SUCESSFUL_ADDED, imprint.length(), 1000);
    }
    We are using Spring and Hibernate for transaction.

    I hope you could help us.

    Thanks
    Alex

  • #2
    Check this https://github.com/bondarenko/forum.springsource-129194 implementation of your issue. It contains test which is passing.

    Comment


    • #3
      Hello buenosdias,

      thanks for your help. I changed my code but the same issue. Maybe it has something to do with the transactionManager? In my Spring config i use
      Code:
      	<bean id="transactionManager"
      		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
      And u used the jpa transactionmanager.

      Comment

      Working...
      X