mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#1568 - @OneToMany with orphanRemoval=true ... with @Cache (and not a @ManyToOne) doesn't remove orphans after cache hit
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package org.tests.model.orphanremoval;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Version;
|
||||
|
||||
@Entity
|
||||
public class OrpDetail2 {
|
||||
|
||||
@Id
|
||||
String id;
|
||||
|
||||
String detail;
|
||||
|
||||
String masterId;
|
||||
|
||||
@Version
|
||||
long version;
|
||||
|
||||
public OrpDetail2(String id, String detail, String masterId) {
|
||||
this.id = id;
|
||||
this.detail = detail;
|
||||
this.masterId = masterId;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getMasterId() {
|
||||
return masterId;
|
||||
}
|
||||
|
||||
public void setMasterId(String masterId) {
|
||||
this.masterId = masterId;
|
||||
}
|
||||
|
||||
public String getDetail() {
|
||||
return detail;
|
||||
}
|
||||
|
||||
public void setDetail(String detail) {
|
||||
this.detail = detail;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package org.tests.model.orphanremoval;
|
||||
|
||||
import io.ebean.annotation.Cache;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Version;
|
||||
import java.util.List;
|
||||
|
||||
@Cache
|
||||
@Entity
|
||||
public class OrpMaster2 {
|
||||
|
||||
@Id
|
||||
String id;
|
||||
|
||||
String name;
|
||||
|
||||
@OneToMany(orphanRemoval = true, cascade = CascadeType.ALL)
|
||||
List<OrpDetail2> details;
|
||||
|
||||
@Version
|
||||
long version;
|
||||
|
||||
public OrpMaster2(String id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<OrpDetail2> getDetails() {
|
||||
return details;
|
||||
}
|
||||
|
||||
public void setDetails(List<OrpDetail2> details) {
|
||||
this.details = details;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package org.tests.model.orphanremoval;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestOrphanRemoveO2MFlat extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testCacheUse() {
|
||||
|
||||
// add test data first
|
||||
OrpMaster2 m0 = new OrpMaster2("m2", "master2");
|
||||
m0.getDetails().add(new OrpDetail2("d21", "d1", "m2"));
|
||||
m0.getDetails().add(new OrpDetail2("d22", "d2", "m2"));
|
||||
|
||||
Ebean.save(m0);
|
||||
|
||||
OrpMaster2 m1 = Ebean.find(OrpMaster2.class, "m2");
|
||||
m1.getDetails().size();
|
||||
|
||||
m1.getDetails().clear();
|
||||
m1.getDetails().add(new OrpDetail2("d23", "d3", "m2"));
|
||||
Ebean.save(m1);
|
||||
|
||||
m1 = Ebean.find(OrpMaster2.class, "m2");
|
||||
// Expect only one.
|
||||
assertThat(m1.getDetails()).hasSize(1);
|
||||
assertThat(m1.getDetails()).extracting("id").containsExactly("d23");
|
||||
|
||||
m1.getDetails().clear();
|
||||
m1.getDetails().add(new OrpDetail2("d24", "d4", "m2"));
|
||||
m1.getDetails().add(new OrpDetail2("d25", "d5", "m2"));
|
||||
Ebean.save(m1);
|
||||
|
||||
m1 = Ebean.find(OrpMaster2.class, "m2");
|
||||
assertThat(m1.getDetails()).hasSize(2);
|
||||
assertThat(m1.getDetails()).extracting("id").containsExactly("d24", "d25");
|
||||
|
||||
|
||||
m1 = Ebean.find(OrpMaster2.class)
|
||||
.setId("m2")
|
||||
.setUseCache(false)
|
||||
.findOne();
|
||||
|
||||
// Expect only one.
|
||||
assertThat(m1.getDetails()).hasSize(2);
|
||||
assertThat(m1.getDetails()).extracting("id").containsExactly("d24", "d25");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user