Results 1 to 3 of 3

Thread: One-To-One relation with lazy fetch

  1. #1
    Join Date
    Aug 2012
    Posts
    2

    Default 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. #2
    Join Date
    Oct 2011
    Posts
    9

    Default

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

  3. #3
    Join Date
    Aug 2012
    Posts
    2

    Default

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •