Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate Spring JPA在实体中存储实体_Hibernate_Spring Data_Spring Data Jpa - Fatal编程技术网

Hibernate Spring JPA在实体中存储实体

Hibernate Spring JPA在实体中存储实体,hibernate,spring-data,spring-data-jpa,Hibernate,Spring Data,Spring Data Jpa,我使用的是SpringDataJPA,后面是hibernate,内存中有H2数据库 我有以下实体: @Entity public class EntityA { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long entityAId; @Column @NotEmpty private String name; @OneToMany(mappedBy

我使用的是SpringDataJPA,后面是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
}
这保存了结果。但是,当我通过
entityAId
使用
findOne
方法从存储库中检索
entityA
时,我得到一个空列表,其中没有任何
EntityB
类型的实例

我甚至尝试通过
entityAId
分别查询
entitybrepositionory
,但我得到了一个空的
entityB
结果列表


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

我相信您缺少级联配置

请试一试

@OneToMany(mappedBy = "entityA", cascade=CascadeType.ALL)
使用以下命令保存它:

entityARepository.save(entityA);
有关级联类型的更多信息,请查看以下内容

编辑: 也可能有一些配置错误。。。您的测试是事务性的吗?下面是一个TestNG代码片段:

@Test
@ContextConfiguration(locations = { "classpath:spring-test-config.xml" })
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@Transactional
public class MyRepositoryTest extends AbstractTransactionalTestNGSpringContextTests {
您的spring配置是否正常?里面有这样的东西吗

    <!-- 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>

com.xxx.domain

我认为您缺少级联配置

请试一试

@OneToMany(mappedBy = "entityA", cascade=CascadeType.ALL)
使用以下命令保存它:

entityARepository.save(entityA);
有关级联类型的更多信息,请查看以下内容

编辑: 也可能有一些配置错误。。。您的测试是事务性的吗?下面是一个TestNG代码片段:

@Test
@ContextConfiguration(locations = { "classpath:spring-test-config.xml" })
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@Transactional
public class MyRepositoryTest extends AbstractTransactionalTestNGSpringContextTests {
您的spring配置是否正常?里面有这样的东西吗

    <!-- 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>

com.xxx.domain

这应该行得通,您的配置中肯定有其他错误。你是如何注射你的全部抗体的?你是怎么测试的?你能给我们看看你的spring配置吗?我已经编辑了我的答案,请检查spring和test-config。不,我没有使用任何spring配置。我将Spring4与
@Repository
注释一起使用。查看详细信息。我正在使用Spring4.1.6和Crudepository,但如果没有答案中的配置,它将无法运行(或者至少我不知道如何在没有它的情况下使其运行)。您的链接还包括spring配置,例如参见1.3第3点。您将在我在回答中发布的配置中找到这部分配置……如果您没有spring配置,您如何将存储库连接到您的测试/服务中?这应该可以工作,您的配置中一定有其他错误。你是如何注射你的全部抗体的?你是怎么测试的?你能给我们看看你的spring配置吗?我已经编辑了我的答案,请检查spring和test-config。不,我没有使用任何spring配置。我将Spring4与
@Repository
注释一起使用。查看详细信息。我正在使用Spring4.1.6和Crudepository,但如果没有答案中的配置,它将无法运行(或者至少我不知道如何在没有它的情况下使其运行)。您的链接还包括spring配置,例如参见1.3第3点。您将在我在回答中发布的配置中找到这部分配置……如果您没有spring配置,您如何将存储库连接到测试/服务中?