Spring JPA 在实体中存储实体

Spring JPA to store entity within entity

我正在使用 Spring 数据 JPA,后面有 Hibernate,内存数据库中有 H2。

我有以下实体:

@Entity
public class EntityA {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long entityAId;

    @Column
    @NotEmpty
    private String name;

    @OneToMany(mappedBy = "entityA")
    private List<EntityB> entityBList;

    // getters and setters
}

@Entity
public class EntityB {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column
    @NotEmpty
    private String name;

    @ManyToOne
    @JoinColumn(referencedColumnName = "entityAId")
    private EntityA entityA;

    // getters and setters
}

这是我的存储库:

@Repository
public interface EntityARepository extends JpaRepository<EntityA, Long> {

}

@Repository
public interface EntityBRepository extends JpaRepository<EntityB, Integer> {
    @Query(value = "FROM EntityB b where b.entityA.entityAId = ?1")
    public List<EntityB> getEntityBByEntityAId(Long entityAId);
}

我的问题是:

如果我有一个 entityA 的实例,里面有 'n' 个 entityB 的实例,如果我调用 entityARepository.save(entityA); 那么它是否也会保存 entityB(是可能吗?)?我试过了,但对我没有用。我试过

entityARepository.save(entityA);
entityBRepository.save(entityBList);

其中保存了结果。但是当我通过 entityAId 使用 findOne 方法从存储库中检索 entityA 时,我得到一个空列表,没有任何 EntityB 类型的实例。

我什至尝试通过 entityAId 单独查询 entityBRepository,但我得到一个空的 entityB 结果列表。

有人可以帮我知道到底缺少什么吗?

我认为您缺少级联配置。

请尝试

@OneToMany(mappedBy = "entityA", cascade=CascadeType.ALL)

使用以下方法保存:

entityARepository.save(entityA);

有关级联类型的更多信息,请查看此 JPA and Hibernate Cascade Types

编辑: 也可能存在一些配置错误……您的测试是事务性的吗?这是一个 TestNG 片段:

@Test
@ContextConfiguration(locations = { "classpath:spring-test-config.xml" })
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@Transactional
public class MyRepositoryTest extends AbstractTransactionalTestNGSpringContextTests {

你的 spring-config 可以吗?它包含这样的内容吗?

    <!-- only components from this package can be wired by spring --> 
    <context:component-scan base-package="com.xxx.*" />

    <!-- Directory to scan for repository classes -->
    <jpa:repositories base-package="com.xxx.domain.repository" />

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${db.driver}" />
        <property name="url" value="${db.url}" />
        <property name="username" value="${db.username}" />
        <property name="password" value="${db.password}" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" >
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

        <property name="dataSource" ref="dataSource" />

        <property name="packagesToScan" >
            <list>
                <value>com.xxx.domain</value>
            </list>
        </property>

        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true" />
                <property name="generateDdl" value="true" />
            </bean>
        </property>

    </bean>