【问题标题】:Entity Merge using Cascade.ALL OnetoMany relationship not inserting records for children使用 Cascade.ALL OnetoMany 关系的实体合并不为子项插入记录
【发布时间】:2020-08-07 10:37:48
【问题描述】:

我已经查看了以前的问题,但所有解决方案仍然不适用于我的项目。

我有一个使用 spring core 5.2.3 的 CUBA 平台项目。 CUBA 使用基于 EclipseLink 框架的 ORM 实现。

我有 1 个主类实体和子类实体。

主类定义

//annotations here
public class MainClass{

    @Composition
    @OnDelete(DeletePolicy.CASCADE)
    @OneToMany(mappedBy = "mainClass", cascade = CascadeType.ALL, orphanRemoval = true)
    protected List<SubClass> subClass;

    public Category getCategory() {
        return category;
    }
}

//子类实体

//annotations here
public class SubClass{

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "MAINCLASS_ID")
    protected MainClass mainClass;
}

这个设置的问题是它只保存了 MainClass 实体,而没有保存 SubClass 实体。

服务类

@Service("MainService")
public class ServiceClass{

    @Inject
    private Persistence persistence;


    @Transactional
    public void saveOrUpdateMain(MainClass mainClass){

       MainClass qMainClass = (MainClass) entityManager.createQuery("select 
           b from main_Class b where b.extID = ?1")
                    .setParameter(1, extID).getSingleResult();

      //assume mainClass is not null, set the primary key of qMainClass to mainClass
      
      mainClass.setId(qMainClass.getId());
      
      entityManager.merge(mainClass);
     

    }


}


我已经阅读了这 2 个链接,但仍然没有解决我的问题。

Why merging is not cascaded on a one to many relationship

JPA does not insert new childs from one to many relationship when we use merge

【问题讨论】:

  • 您在运行时使用哪个 ORM 实现以及在哪个版本中?请将该信息添加到问题中,以便其他人有机会正确重现您的设置(问题)。
  • 显示执行实体保存的代码
  • 我已经更新了这个问题。 CUBA 使用基于 EclipseLink 框架的 ORM 实现。谢谢

标签: java jpa spring-data-jpa cuba-platform


【解决方案1】:

在 CUBA 中,DataManager 是处理数据的首选选项。它自动解析级联操作,不需要明确的事务定义。请先尝试使用 DataManager 实现此逻辑。

【讨论】:

    猜你喜欢
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 2019-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-26
    • 1970-01-01
    相关资源
    最近更新 更多