mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Test case and fix for #113 (395) : On save @OneToOne does not cascade parent IDs to child node
This commit is contained in:
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user