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:
AccountMainSettings: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; }
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.Code:@OneToOne(mappedBy = "accountMainSettings") private AccountMain accountMain; public final AccountMain getAccountMain() { return this.accountMain; }
How can we implement lazy loading in this context?
Here our jUnit Test:
We are using Spring and Hibernate for transaction.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); }
I hope you could help us.
Thanks
Alex


Reply With Quote
