Test case and fix for #113 (395) : On save @OneToOne does not cascade parent IDs to child node

This commit is contained in:
Rob Bygrave
2014-04-30 00:01:54 +12:00
parent 5049694fd3
commit 563293f7a9
5 changed files with 136 additions and 0 deletions
@@ -0,0 +1,42 @@
package com.avaje.tests.model.onetoone;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class OtoChild {
@Id
Integer id;
String name;
@OneToOne
OtoMaster master;
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 OtoMaster getMaster() {
return master;
}
public void setMaster(OtoMaster master) {
this.master = master;
}
}
@@ -0,0 +1,43 @@
package com.avaje.tests.model.onetoone;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class OtoMaster {
@Id
Long id;
String name;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "master")
OtoChild child;
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 OtoChild getChild() {
return child;
}
public void setChild(OtoChild child) {
this.child = child;
}
}
@@ -0,0 +1,36 @@
package com.avaje.tests.model.onetoone;
import org.junit.Assert;
import org.junit.Test;
import com.avaje.ebean.BaseTestCase;
import com.avaje.ebean.Ebean;
public class TestOneToOneCascadeSave extends BaseTestCase {
@Test
public void test() {
OtoMaster master = new OtoMaster();
master.setName("CName");
OtoChild child = new OtoChild();
child.setName("OName");
master.setChild(child);
// The parent customer object should be automatically set onto the child
// object if it is currently null so you don't need to do the extra
// o.setCustomer(c);
Ebean.save(master);
Assert.assertNotNull(child.getId());
OtoChild child2 = Ebean.find(OtoChild.class, child.getId());
OtoMaster master2 = child2.getMaster();
Assert.assertNotNull(master2);
}
}