1

我正在使用 Spring Data(JPA 风格)和 Hibernate 为新的 Web 应用程序构建数据层。我遇到了@OneToMany 集合没有填充数据加载的问题。我试过 FetchType.LAZY 和 FetchType.EAGER,结果没有变化。这些注释是否设置正确?

这是父类(为空间而编辑):

public class AppMaster implements java.io.Serializable {

    @Id
    @GeneratedValue
    @Column(name="APPLICATION_ID", unique=true, nullable=false)
     private long ApplicationId;

    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="appMaster")
     private Set<AppAddress> addresses = new HashSet<AppAddress>(0);

这是子类(也为空间编辑):

public class AppAddress implements java.io.Serializable {

    @Id
    @GeneratedValue
    @Column(name="ADDRESS_ID", unique=true, nullable=false)
     private long addressId;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="APPLICATION_ID", nullable=false)
     private AppMaster appMaster;

我的数据是在 Junit 测试的 @Before 设置部分生成并保存的,如下所示:

AppMaster table:  
    APPLICATION_ID=1

AppAddress table:
    ADDRESS_ID=1, APPLICATION_ID=1
    ADDRESS_ID=2, APPLICATION_ID=1
    ADDRESS_ID=3, APPLICATION_ID=1
    ADDRESS_ID=4, APPLICATION_ID=1

所以我应该可以说

AppMaster master = appMasterService.findOne(1);  // Load existing record
assertEquals(4, master.getAddresses().size());   //  Loaded 4 dependent records

但是当我这样做时,我得到了这个:

java.lang.AssertionError: 
Expected :4
Actual   :0
    at org.junit.Assert.fail(Assert.java:93)
    at org.junit.Assert.failNotEquals(Assert.java:647)
    at org.junit.Assert.assertEquals(Assert.java:128)
    at org.junit.Assert.assertEquals(Assert.java:472)
    at org.junit.Assert.assertEquals(Assert.java:456)
    at test.service.AppAddressServiceTest.setupData(AppAddressServiceTest.java:45)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    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.RunBefores.evaluate(RunBefores.java:27)
    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.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)

任何人都可以对此有所了解吗?

4

2 回答 2

4

问题是原始数据是在同一个事务中创建和读取的。由于在事务结束时数据从未刷新到实际数据存储中,因此 JUnit 测试(这是出现此问题的地方)没有任何内容可供读取。

解决方法是在spring test context.xml中创建HSQLDB数据库启动时的测试数据:

<jdbc:embedded-database id="testDataSource" type="HSQL">
    <jdbc:script location="database/app_schema.sql" />
    <jdbc:script location="database/app_data.sql" />
</jdbc:embedded-database>

这样,当 JUnit 测试启动时,那里已经有数据等待读取。

于 2013-08-07T19:59:21.630 回答
0

您是否启用了调试模式以显示生成的 sql?在 Spring 中,它是 showSQL=true。看看 SQL,它可能会提供一些线索。

于 2013-07-30T17:20:10.840 回答