mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Merge branch 'FOCONIS-bug-save-one-to-one-version-cascade'
This commit is contained in:
@@ -360,7 +360,7 @@ public final class EntityBeanIntercept implements Serializable {
|
||||
* Return true if the entity should be updated.
|
||||
*/
|
||||
public boolean isUpdate() {
|
||||
return forceUpdate || state == STATE_LOADED;
|
||||
return forceUpdate || state == STATE_LOADED || state == STATE_REFERENCE;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package org.tests.model.onetoone;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Version;
|
||||
|
||||
import io.ebean.annotation.Where;
|
||||
|
||||
@Entity
|
||||
public class OtoChildVersion {
|
||||
|
||||
@Id
|
||||
Integer id;
|
||||
|
||||
String name;
|
||||
|
||||
@OneToOne
|
||||
OtoMasterVersion master;
|
||||
|
||||
@Version
|
||||
int version;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL)
|
||||
@JoinColumn(name = "ref_id")
|
||||
@Where(clause = "${mta}.type=1")
|
||||
List<OtoNotification> notifications;
|
||||
|
||||
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 OtoMasterVersion getMaster() {
|
||||
return master;
|
||||
}
|
||||
|
||||
public void setMaster(OtoMasterVersion master) {
|
||||
this.master = master;
|
||||
}
|
||||
|
||||
public int getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(int version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package org.tests.model.onetoone;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Version;
|
||||
|
||||
import io.ebean.annotation.Where;
|
||||
|
||||
@Entity
|
||||
public class OtoMasterVersion {
|
||||
|
||||
@Id
|
||||
Long id;
|
||||
|
||||
String name;
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL, mappedBy = "master")
|
||||
OtoChildVersion child;
|
||||
|
||||
@Version
|
||||
int version;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL)
|
||||
@JoinColumn(name = "ref_id")
|
||||
@Where(clause = "${mta}.type=0")
|
||||
List<OtoNotification> notifications;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public OtoChildVersion getChild() {
|
||||
return child;
|
||||
}
|
||||
|
||||
public void setChild(OtoChildVersion child) {
|
||||
this.child = child;
|
||||
}
|
||||
|
||||
public int getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(int version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package org.tests.model.onetoone;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class OtoNotification {
|
||||
|
||||
@Id
|
||||
Integer id;
|
||||
|
||||
Integer refId;
|
||||
|
||||
// 0 = master, 1 = child
|
||||
Integer type;
|
||||
|
||||
String text;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Integer getRefId() {
|
||||
return refId;
|
||||
}
|
||||
|
||||
public void setRefId(Integer refId) {
|
||||
this.refId = refId;
|
||||
}
|
||||
|
||||
public Integer getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(Integer type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public void setText(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,8 +2,11 @@ package org.tests.model.onetoone;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Transaction;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class TestOneToOneCascadeSave extends BaseTestCase {
|
||||
@@ -29,5 +32,39 @@ public class TestOneToOneCascadeSave extends BaseTestCase {
|
||||
OtoMaster master2 = child2.getMaster();
|
||||
assertNotNull(master2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSaveCascadeWithOneToOne() {
|
||||
OtoMasterVersion master = new OtoMasterVersion();
|
||||
master.setName("m1");
|
||||
|
||||
OtoChildVersion child = new OtoChildVersion();
|
||||
child.setName("c1");
|
||||
|
||||
master.setChild(child);
|
||||
DB.save(master);
|
||||
|
||||
assertThat(master.getVersion()).isEqualTo(1);
|
||||
assertThat(child.getVersion()).isEqualTo(1);
|
||||
|
||||
child.setName("c2");
|
||||
DB.save(master);
|
||||
|
||||
assertThat(master.getVersion()).isEqualTo(1);
|
||||
assertThat(child.getVersion()).isEqualTo(2);
|
||||
|
||||
try (Transaction txn = DB.beginTransaction()) {
|
||||
master = DB.find(OtoMasterVersion.class).findOne();
|
||||
|
||||
master.setName("m2");
|
||||
DB.save(master);
|
||||
child = DB.find(OtoChildVersion.class).findOne();
|
||||
assertThat(child.getVersion()).isEqualTo(2);
|
||||
child.setName("c3");
|
||||
DB.save(child);
|
||||
|
||||
txn.commit();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user