[英]Nested objects have not been saved by Spring JpaRepository
我有一個帶有 GWT 的 Spring Roo 應用程序。 在服務器端,我為所有實體提供了簡單的 JpaRepository 接口,例如:
@Repository
public interface MyEntityRepository extends JpaSpecificationExecutor<MyEntity>, JpaRepository<MyEntity, Long> {
}
有一個與 MyOtherEntity 類具有一對一關系的 MyEntity 類。 當我調用我的實體服務持久方法時
public void saveMyEntity (MyEntity myEntity) {
myEntityRepository.save(myEntity);
}
只會保存 myEntity 對象。 MyEntity 的所有子對象都將被忽略。 將 myEntity 對象與 myOtherEntity 對象一起保存的唯一方法是調用
myOtherEntityRepository.save(myOtherEntity);
在上面的代碼之前。 那么有沒有更優雅的方式來自動保存帶有 JpaRepository 接口的子對象呢?
我不知道你的實現細節。 但是,我認為,它只需要在JPA
使用CascadeType
。 JPA 參考CascadeType 。
嘗試如下。
public class MyEntity {
@OneToOne(cascade=CascadeType.PERSIST) <or> @OneToOne(cascade=CascadeType.ALL) <-- for all operation
@JoinColumn(name = "YOUR-ID")
private MyOtherEntity myOtherEntity ;
}
對於遞歸 MyEntity 關系
public class MyEntity {
@OneToOne(cascade=CascadeType.PERSIST) <or> @OneToOne(cascade=CascadeType.ALL) <-- for all operation
@JoinColumn(name = "YOUR-ID")
private MyEntity myEntity ;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.