#816 - Error saving circular relationships (when generated Id values)

This commit is contained in:
Robin Bygrave
2016-08-25 22:44:22 +12:00
parent d1e2176a5a
commit 244275fd72
20 changed files with 358 additions and 135 deletions
@@ -26,10 +26,6 @@ public class Booking {
@Version
private int version;
public Booking(Long bookingUid) {
this.bookingUid = bookingUid;
}
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "agent_invoice")
private Invoice agentInvoice;
@@ -40,7 +36,11 @@ public class Booking {
@OneToMany(mappedBy = "booking")
private List<Invoice> invoices;
public Booking(Long bookingUid) {
this.bookingUid = bookingUid;
}
public Long getId() {
return id;
}
@@ -12,7 +12,6 @@ public class TestOne2OneBookingInvoice extends BaseTestCase {
public void test() {
Booking b = new Booking(3000L);
Ebean.save(b);
Invoice ai = new Invoice();
Invoice ci = new Invoice();
@@ -0,0 +1,83 @@
package com.avaje.tests.model.m2o;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Version;
@Entity
public class Addr {
@Id
Long id;
@ManyToOne
Empl employee;
String name;
String addressLine1;
String addressLine2;
String city;
@Version
long version;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Empl getEmployee() {
return employee;
}
public void setEmployee(Empl employee) {
this.employee = employee;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddressLine1() {
return addressLine1;
}
public void setAddressLine1(String addressLine1) {
this.addressLine1 = addressLine1;
}
public String getAddressLine2() {
return addressLine2;
}
public void setAddressLine2(String addressLine2) {
this.addressLine2 = addressLine2;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public long getVersion() {
return version;
}
public void setVersion(long version) {
this.version = version;
}
}
@@ -0,0 +1,65 @@
package com.avaje.tests.model.m2o;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import java.util.List;
@Entity
public class Empl {
@Id
Long id;
String name;
Integer age;
@OneToMany(mappedBy="employee" , cascade= CascadeType.ALL)
List<Addr> addresses;
@ManyToOne(cascade = CascadeType.ALL)
Addr defaultAddress;
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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public List<Addr> getAddresses() {
return addresses;
}
public void setAddresses(List<Addr> addresses) {
this.addresses = addresses;
}
public Addr getDefaultAddress() {
return defaultAddress;
}
public void setDefaultAddress(Addr defaultAddress) {
this.defaultAddress = defaultAddress;
}
}
@@ -0,0 +1,49 @@
package com.avaje.tests.model.m2o;
import com.avaje.ebean.BaseTestCase;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.annotation.Transactional;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class TestManyToOneAsOne extends BaseTestCase {
@Test
public void test_when_nonJdbcBatch() {
runInserts();
}
@Transactional(batchSize = 20)
@Test
public void test_when_jdbcBatch() {
runInserts();
}
private void runInserts() {
Addr junk = new Addr();
junk.setName("junk");
Ebean.save(junk);
Empl emp = new Empl();
emp.setName("My Name");
Addr address = new Addr();
address.setName("home");
emp.getAddresses().add(address);
// if I do an interim Ebean.save here it works
//Ebean.save(emp);
address.setEmployee(emp);
emp.setDefaultAddress(address);
Ebean.save(emp);
assertThat(emp.getDefaultAddress().getId()).isNotNull();
assertThat(address.getEmployee().getId()).isNotNull();
Empl foundEmpl = Ebean.find(Empl.class, emp.getId());
assertThat(foundEmpl.getDefaultAddress().getId()).isEqualTo(address.getId());
}
}