mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#994 - @OneToOne cacade does not honor orphanRemoval = true
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
package org.tests.model.onetoone;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Version;
|
||||
|
||||
@Entity
|
||||
public class OtoCust {
|
||||
|
||||
@Id
|
||||
long cid;
|
||||
|
||||
String name;
|
||||
|
||||
/**
|
||||
* Orphan removal so must delete 'old' address when it has been replace or set to null.
|
||||
*/
|
||||
@OneToOne(mappedBy = "customer", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
OtoCustAddress address;
|
||||
|
||||
@Version
|
||||
long version;
|
||||
|
||||
public OtoCust(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getCid() {
|
||||
return cid;
|
||||
}
|
||||
|
||||
public void setCid(long cid) {
|
||||
this.cid = cid;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public OtoCustAddress getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(OtoCustAddress address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package org.tests.model.onetoone;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Version;
|
||||
|
||||
@Entity
|
||||
public class OtoCustAddress {
|
||||
|
||||
@Id
|
||||
long aid;
|
||||
|
||||
String line1;
|
||||
String line2;
|
||||
String line3;
|
||||
|
||||
@OneToOne
|
||||
OtoCust customer;
|
||||
|
||||
@Version
|
||||
long version;
|
||||
|
||||
public OtoCustAddress(String line1, String line2) {
|
||||
this.line1 = line1;
|
||||
this.line2 = line2;
|
||||
}
|
||||
|
||||
public long getAid() {
|
||||
return aid;
|
||||
}
|
||||
|
||||
public void setAid(long aid) {
|
||||
this.aid = aid;
|
||||
}
|
||||
|
||||
public OtoCust getCustomer() {
|
||||
return customer;
|
||||
}
|
||||
|
||||
public void setCustomer(OtoCust customer) {
|
||||
this.customer = customer;
|
||||
}
|
||||
|
||||
public String getLine1() {
|
||||
return line1;
|
||||
}
|
||||
|
||||
public void setLine1(String line1) {
|
||||
this.line1 = line1;
|
||||
}
|
||||
|
||||
public String getLine2() {
|
||||
return line2;
|
||||
}
|
||||
|
||||
public void setLine2(String line2) {
|
||||
this.line2 = line2;
|
||||
}
|
||||
|
||||
public String getLine3() {
|
||||
return line3;
|
||||
}
|
||||
|
||||
public void setLine3(String line3) {
|
||||
this.line3 = line3;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package org.tests.model.onetoone;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestOneToOneOrphanRemove extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void base() {
|
||||
|
||||
OtoCust jack = new OtoCust("Jack");
|
||||
|
||||
OtoCustAddress address = new OtoCustAddress("line1", "line2");
|
||||
|
||||
jack.setAddress(address);
|
||||
Ebean.save(jack);
|
||||
|
||||
// set new address
|
||||
OtoCustAddress address2 = new OtoCustAddress("other1", "other2");
|
||||
jack.setAddress(address2);
|
||||
|
||||
// Fail do to uniqueness constraint
|
||||
LoggedSqlCollector.start();
|
||||
Ebean.save(jack);
|
||||
|
||||
List<String> sql = LoggedSqlCollector.current();
|
||||
assertThat(sql).hasSize(3);
|
||||
assertThat(sql.get(0)).contains("delete from oto_cust_address where aid=? and version=?");
|
||||
assertThat(sql.get(1)).contains("update oto_cust set version=? where cid=? and version=?");
|
||||
assertThat(sql.get(2)).contains("insert into oto_cust_address (line1, line2, line3, version, customer_cid)");
|
||||
|
||||
jack.setAddress(null);
|
||||
Ebean.save(jack);
|
||||
|
||||
sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(2);
|
||||
assertThat(sql.get(0)).contains("delete from oto_cust_address where aid=? and version=?");
|
||||
assertThat(sql.get(1)).contains("update oto_cust set version=? where cid=? and version=?");
|
||||
|
||||
OtoCustAddress foundAddress = Ebean.find(OtoCustAddress.class, address2.getAid());
|
||||
assertThat(foundAddress).isNull();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user