0

I already looked at previous questions but all the solutions still doesn't work on my project.

I have a CUBA Platform project that uses spring core 5.2.3. CUBA uses the ORM implementation based on the EclipseLink framework.

I have 1 MainClass Entity, and children, SubClass Entity.

MainClass Definition

//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;
    }
}

//SubClass entity

//annotations here
public class SubClass{

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

The problem with this setup is that it only saves the MainClass Entity but not the SubClass Entity.

Service Class

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

    }


}


I have read this 2 links but still did not solve my issue.

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

NothingBox
  • 345
  • 5
  • 15
  • Which ORM implementation do you use at runtime and in which version? Please add that information to the question, so others have a chance to reproduce your setup (problem) correctly. – MWiesner Aug 07 '20 at 12:08
  • Show the code that performs the saving of the entities – perissf Aug 07 '20 at 13:49
  • I have updated the question. CUBA uses the ORM implementation based on the EclipseLink framework. Thank you – NothingBox Aug 08 '20 at 08:11

1 Answers1

0

In CUBA, DataManager is a preferred option to work with data. It automatically resolves cascade operations and does not require explicit transaction definition. Please try to implement this logic using DataManager first.