mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-12 22:41:35 +00:00
#2458 - Add BeanPersistController.isCascade() to identify cascade vs "top level" persist requests
This commit is contained in:
1 parent
dd43fe4db3
commit
1a6a426683
7 files changed
+154
-18
No files matched your search
@@ -5,13 +5,15 @@ import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.TWithPreInsert;
|
||||
import org.tests.model.basic.TWithPreInsertChild;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class TestPreInsertValidation extends BaseTestCase {
|
||||
class TestPreInsertValidation extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
void test() {
|
||||
|
||||
TWithPreInsert e = new TWithPreInsert();
|
||||
e.setTitle("Mister");
|
||||
@@ -22,15 +24,45 @@ public class TestPreInsertValidation extends BaseTestCase {
|
||||
// the save worked and name set in preInsert
|
||||
assertNotNull(e.getId());
|
||||
assertNotNull(e.getName());
|
||||
assertThat(e.requestCascadeState()).isEqualTo(2);
|
||||
|
||||
TWithPreInsert e1 = DB.find(TWithPreInsert.class, e.getId());
|
||||
|
||||
e1.setTitle("Missus");
|
||||
DB.save(e1);
|
||||
|
||||
assertThat(e1.requestCascadeState()).isEqualTo(12);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStatelessUpdate() {
|
||||
void test_cascade() {
|
||||
|
||||
TWithPreInsert e = new TWithPreInsert();
|
||||
e.setTitle("ParentCascading");
|
||||
e.children().add(new TWithPreInsertChild("Child0"));
|
||||
// the perInsert should populate the
|
||||
// name with should not be null
|
||||
DB.save(e);
|
||||
|
||||
// the save worked and name set in preInsert
|
||||
assertNotNull(e.getId());
|
||||
assertNotNull(e.getName());
|
||||
assertThat(e.requestCascadeState()).isEqualTo(2);
|
||||
assertThat(e.children().get(0).requestCascadeState()).isEqualTo(1);
|
||||
|
||||
TWithPreInsert e1 = DB.find(TWithPreInsert.class, e.getId());
|
||||
assert e1 != null;
|
||||
|
||||
e1.setTitle("ParentCascading-changed");
|
||||
e1.children().get(0).setName("Child0-changed");
|
||||
DB.save(e1);
|
||||
|
||||
assertThat(e1.requestCascadeState()).isEqualTo(12);
|
||||
assertThat(e1.children().get(0).requestCascadeState()).isEqualTo(11);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testStatelessUpdate() {
|
||||
|
||||
TWithPreInsert e = new TWithPreInsert();
|
||||
e.setName("BeanForUpdateTest");
|
||||
|
||||
@@ -2,10 +2,15 @@ package org.tests.model.basic;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static javax.persistence.CascadeType.PERSIST;
|
||||
|
||||
@Entity
|
||||
public class TWithPreInsert {
|
||||
public class TWithPreInsert implements TWithPreInsertCommon {
|
||||
|
||||
@Id
|
||||
private Integer id;
|
||||
@@ -15,6 +20,14 @@ public class TWithPreInsert {
|
||||
|
||||
private String title;
|
||||
|
||||
@OneToMany(cascade = PERSIST)
|
||||
private List<TWithPreInsertChild> children = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* For testing what happened in persist controller.
|
||||
*/
|
||||
transient int requestCascadeState;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
@@ -39,4 +52,15 @@ public class TWithPreInsert {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public List<TWithPreInsertChild> children() {
|
||||
return children;
|
||||
}
|
||||
|
||||
public int requestCascadeState() {
|
||||
return requestCascadeState;
|
||||
}
|
||||
|
||||
public void requestCascadeState(int requestCascadeState) {
|
||||
this.requestCascadeState = requestCascadeState;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package org.tests.model.basic;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
@Entity
|
||||
public class TWithPreInsertChild implements TWithPreInsertCommon {
|
||||
|
||||
@Id
|
||||
private Integer id;
|
||||
|
||||
@NotNull
|
||||
private String name;
|
||||
|
||||
private String title;
|
||||
|
||||
/**
|
||||
* For testing what happened in persist controller.
|
||||
*/
|
||||
transient int requestCascadeState;
|
||||
|
||||
public TWithPreInsertChild(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public int requestCascadeState() {
|
||||
return requestCascadeState;
|
||||
}
|
||||
|
||||
public void requestCascadeState(int requestCascadeState) {
|
||||
this.requestCascadeState = requestCascadeState;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package org.tests.model.basic;
|
||||
|
||||
public interface TWithPreInsertCommon {
|
||||
|
||||
String getName();
|
||||
|
||||
void setName(String name);
|
||||
|
||||
String getTitle();
|
||||
|
||||
void setTitle(String title);
|
||||
|
||||
void requestCascadeState(int requestCascadeState);
|
||||
}
|
||||
+11
-14
@@ -2,36 +2,33 @@ package org.tests.model.basic.event;
|
||||
|
||||
import io.ebean.event.BeanPersistAdapter;
|
||||
import io.ebean.event.BeanPersistRequest;
|
||||
import org.tests.model.basic.TWithPreInsert;
|
||||
import org.tests.model.basic.TWithPreInsertCommon;
|
||||
|
||||
public class TWithPreInsertPersistAdapter extends BeanPersistAdapter {
|
||||
|
||||
@Override
|
||||
public boolean isRegisterFor(Class<?> cls) {
|
||||
return TWithPreInsert.class.equals(cls);
|
||||
return TWithPreInsertCommon.class.isAssignableFrom(cls);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean preInsert(BeanPersistRequest<?> request) {
|
||||
|
||||
TWithPreInsert e = (TWithPreInsert) request.bean();
|
||||
if (e.getName() == null) {
|
||||
e.setName("set on preInsert");
|
||||
TWithPreInsertCommon bean = (TWithPreInsertCommon) request.bean();
|
||||
if (bean.getName() == null) {
|
||||
bean.setName("set on preInsert");
|
||||
}
|
||||
bean.requestCascadeState(request.isCascade() ? 1 : 2);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean preUpdate(BeanPersistRequest<?> request) {
|
||||
|
||||
TWithPreInsert b = (TWithPreInsert) request.bean();
|
||||
System.out.println("preUpdate - title is: " + b.getTitle());
|
||||
if (b.getTitle() == null) {
|
||||
b.setTitle("set on preUpdate");
|
||||
TWithPreInsertCommon bean = (TWithPreInsertCommon) request.bean();
|
||||
System.out.println("preUpdate - title is: " + bean.getTitle());
|
||||
if (bean.getTitle() == null) {
|
||||
bean.setTitle("set on preUpdate");
|
||||
}
|
||||
// request.getEbeanServer().refresh(b);
|
||||
// System.out.println("title is Mister:"+b.getTitle());
|
||||
|
||||
bean.requestCascadeState(request.isCascade() ? 11 : 12);
|
||||
return super.preUpdate(request);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user