簡體   English   中英

Spring JpaRepository 尚未保存嵌套對象

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM