在我们的Java应用程序中,我们有两个实体:主帐户和此帐户的设置。我们使用hibernate来提供持久性。我们希望帐户设置延迟加载。所以我们这样做了:
AccountMain:
@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:
@OneToOne(mappedBy = "accountMainSettings")
private AccountMain accountMain;
public final AccountMain getAccountMain() {
return this.accountMain;
}
当我们在AccountMain中加载AccountMainSettings对象时,它应该被代理。但是当我们调用AccountMainSettings的方法时,该对象不会从数据库加载,当然会引入NPE。 阅读Making-a-onetoone-relation-lazy并没有多大帮助。我们既没有可空的关联也不想将它转换为ManyToOne关联。当我们切换到急切加载时,问题被“解决”,因为设置已加载但它们包含许多字段,因此我们不希望它们被不必要地加载。
我们如何在这种情况下实现延迟加载?
更新:这里是我们的jUnit测试:
@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);
}
Stacktrace:
java.lang.NullPointerException
at com.persolog.eport.service.dao.AccountMainDAOTest.getMainAccountByAccountId(AccountMainDAOTest.java:418)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
答案 0 :(得分:0)
@JoinColumn
注释,而不是在getter上。或者使用getter的所有映射,然后添加@Access(AccessType.PROPERTY)
答案 1 :(得分:0)
我有这个,它在我的代码中适用于我。
@JoinColumn(name = "contract_id", referencedColumnName = "id")
@OneToOne(fetch = FetchType.LAZY)
private Contract contract;
您是否遗漏了代码中的referencedColumnName?您指定了连接列的名称,但没有指定关系另一侧的列名称
请注意我的所有注释都在该字段上,而我的getter和setter只是该类中的普通旧getter和setter