Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: #3142 Cascading a OneToMany, set the relationship when the child isn't new or dirty #3366

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -588,10 +588,9 @@ public TableJoin intersectionTableJoin() {
}

/**
* Set the join properties from the parent bean to the child bean.
* This is only valid for OneToMany and NOT valid for ManyToMany.
* Set the parent bean to the child (update the relationship).
*/
public void setJoinValuesToChild(EntityBean parent, EntityBean child, Object mapKeyValue) {
public void setParentToChild(EntityBean parent, EntityBean child, Object mapKeyValue) {
if (mapKeyProperty != null) {
mapKeyProperty.setValue(child, mapKeyValue);
}
Expand All @@ -602,6 +601,31 @@ public void setJoinValuesToChild(EntityBean parent, EntityBean child, Object map
}
}

/**
* Return true if the parent bean has been set to the child (updated the relationship).
*/
public boolean setParentToChild(EntityBean parent, EntityBean child, Object mapKeyValue, BeanDescriptor<?> parentDesc) {
if (manyToMany
|| childMasterProperty == null
|| !child._ebean_getIntercept().isLoadedProperty(childMasterProperty.propertyIndex())) {
return false;
}

Object currentParent = childMasterProperty.getValue(child);
if (currentParent != null) {
Object newId = parentDesc.getId(parent);
Object oldId = parentDesc.id(currentParent);
if (Objects.equals(newId, oldId)) {
return false;
}
}
childMasterProperty.setValueIntercept(child, parent);
if (mapKeyProperty != null) {
mapKeyProperty.setValue(child, mapKeyValue);
}
return true;
}

/**
* Return the order by clause used to order the fetching of the data for
* this list, set or map.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ private void saveAllBeans(final BeanProperty orderColumn) {
} else if (ebi.isNewOrDirty()) {
skipSavingThisBean = false;
// set the parent bean to detailBean
many.setJoinValuesToChild(parentBean, detail, mapKeyValue);
many.setParentToChild(parentBean, detail, mapKeyValue);
} else if (many.setParentToChild(parentBean, detail, mapKeyValue, request.descriptor())) {
skipSavingThisBean = false;
} else {
skipSavingThisBean = saveRecurseSkippable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import org.tests.model.basic.TSDetail;
import org.tests.model.basic.TSMaster;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;

public class TestSaveAssociation extends BaseTestCase {
class TestSaveAssociation extends BaseTestCase {

@Test
public void test() {
void test() {

TSMaster m0 = new TSMaster();
m0.setName("master1");
Expand All @@ -26,7 +26,57 @@ public void test() {
TSMaster m0Check = DB.find(TSMaster.class).fetch("details").where().idEq(m0.getId())
.findOne();

assertEquals(2, m0Check.getDetails().size());
assertThat(m0Check.getDetails()).hasSize(2);
DB.delete(m0);
}


@Test
void testCascadeSetParent() {
// setup
TSDetail detail = new TSDetail("master1 detail1");
DB.save(detail);

// act
TSMaster m0 = new TSMaster();
m0.setName("master2");
m0.addDetail(detail);
DB.save(m0);

// assert
TSMaster check = DB.find(TSMaster.class).fetch("details")
.where().idEq(m0.getId())
.findOne();

assertThat(check.getDetails()).hasSize(1);
assertThat(check.getDetails().get(0).getId()).isEqualTo(detail.getId());
DB.delete(m0);
}

@Test
void testCascadeChangeParent() {
// setup
TSDetail detail = new TSDetail("master3 detail1");
TSMaster m0 = new TSMaster();
m0.setName("master3");
m0.addDetail(detail);
DB.save(m0);

// act
TSMaster m1 = new TSMaster();
m1.setName("master4");
m1.addDetail(detail);
DB.save(m1);

// assert
TSMaster check = DB.find(TSMaster.class).fetch("details")
.where().idEq(m1.getId())
.findOne();

assertThat(check.getDetails()).hasSize(1);
assertThat(check.getDetails().get(0).getId()).isEqualTo(detail.getId());

DB.delete(m1);
DB.delete(TSMaster.class, m0.getId());
}
}
Loading