2016-03-01 60 views
0

我在內存數據庫中使用帶有hibernate的Spring數據JPA。Spring JPA將實體存儲在實體內

我有以下實體:

@Entity 
public class EntityA { 
@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
private Long entityAId; 
@Column 
@NotEmpty 
private String name; 
@OneToMany(mappedBy = "entityA") 
private List<EntityB> entityBList; 
//getter 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; 
//getter and setter 

這裏是我的倉庫:

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

哪個保存了結果。但是當我檢索entityA使用findOne方法從存儲庫由entityAId,我得到一個空列表沒有任何EntityB類型的實例。

我甚至試圖通過entityAId分別查詢entityBRepository,但我得到一個空的entityB結果列表。

有人可以請幫我知道究竟是什麼遺漏嗎?

回答

2

我相信你錯過了Cascade-Configuration。

請嘗試

@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 { 

是您的彈簧配置好嗎?它是否包含這樣的東西?

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

謝謝,但不爲我工作:( – SMA

+0

這應該工作,別的東西一定是錯在你的配置。你怎麼注入你entityARepository?你怎麼測試呢?你可以向我們展示你的彈簧配置? – user3227576

+0

我編輯了我的答案,請檢查彈簧和測試配置 – user3227576