0

在我们的 java 应用程序中,我们有两个实体:一个主帐户和该帐户的设置。我们使用休眠来提供持久性。我们希望帐户设置延迟加载。所以我们这样做了:

主账户:

@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;
}

帐户主要设置:

@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);
}

和堆栈跟踪:

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)
4

2 回答 2

0

吸气剂不应该是最终的。并在字段上使用@JoinColumn注释,而不是在 getter 上。或者使用 getter 的所有映射,那么你应该添加@Access(AccessType.PROPERTY)

于 2012-08-09T10:08:40.190 回答
0

我有这个,它在我的代码中对我有用。

@JoinColumn(name = "contract_id", referencedColumnName = "id")
@OneToOne(fetch = FetchType.LAZY)
private Contract contract;

您是否缺少代码中的 referencedColumnName ?您正在指定连接列的名称,而不是关系另一侧的列名称

请注意,我的所有注释都在该字段上,而我的 getter 和 setter 只是此类中普通的旧 getter 和 setter

于 2012-08-09T10:09:30.830 回答