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