Support @ManyToOne inside @Embeddables - fix for #52 (#1608)

* #52 - @ManyToOne support inside @Embeddables

Initial test and work

* #52 - @ManyToOne support inside @Embeddables

Initial test and work

* #52 - @ManyToOne support inside @Embeddables

Initial test and work

* #52 - @ManyToOne support inside @Embeddables

Initial test and work

* #52 - @ManyToOne support inside @Embeddables

Initial test and work

* #52 - @ManyToOne support inside @Embeddables

Initial test and work

* #52 - @ManyToOne support inside @Embeddables

Update test with sql capture

* #52 - @ManyToOne support inside @Embeddables

Update test with cache clearing before hand (such that we hit DB on lazy load)
This commit is contained in:
Rob Bygrave
2019-01-10 03:40:02 +13:00
committed by GitHub
parent 2b0669c2ae
commit 6b27a9d280
20 changed files with 356 additions and 69 deletions
@@ -0,0 +1,57 @@
package org.tests.model.embedded;
import org.tests.model.basic.Country;
import javax.persistence.Embeddable;
import javax.persistence.ManyToOne;
@Embeddable
public class EAddr {
String street;
String suburb;
String city;
@ManyToOne
Country country;
public EAddr(String street, String city, Country country) {
this.street = street;
this.city = city;
this.country = country;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getSuburb() {
return suburb;
}
public void setSuburb(String suburb) {
this.suburb = suburb;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
}
@@ -0,0 +1,58 @@
package org.tests.model.embedded;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Version;
@Entity
public class EPerAddr {
@Id
Long id;
@Version
Long version;
String name;
@Embedded(prefix = "ma_")
EAddr address;
public EPerAddr(String name, EAddr address) {
this.name = name;
this.address = address;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public EAddr getAddress() {
return address;
}
public void setAddress(EAddr address) {
this.address = address;
}
}
@@ -0,0 +1,56 @@
package org.tests.model.embedded;
import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import io.ebean.plugin.BeanType;
import io.ebean.plugin.Property;
import org.ebeantest.LoggedSqlCollector;
import org.junit.Test;
import org.tests.model.basic.Country;
import org.tests.model.basic.ResetBasicData;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class TestEmbeddedManyToOne extends BaseTestCase {
@Test
public void test() {
ResetBasicData.reset();
BeanType<EAddr> embType = Ebean.getDefaultServer().getPluginApi().getBeanType(EAddr.class);
Ebean.getDefaultServer().getServerCacheManager().clearAll();
Country nz = Ebean.getReference(Country.class, "NZ");
EAddr addr = new EAddr("Foo", "Bar", nz);
EPerAddr perAddr = new EPerAddr("Embed", addr);
Property country = embType.getProperty("country");
Object val = country.getVal(addr);
assertThat(val).isSameAs(nz);
Ebean.save(perAddr);
LoggedSqlCollector.start();
EPerAddr found = Ebean.find(EPerAddr.class, perAddr.getId());
assertThat(found.getAddress().getCountry().getCode()).isEqualTo("NZ");
assertThat(found.getAddress().getCountry().getName()).startsWith("New");
List<String> sql = LoggedSqlCollector.stop();
assertThat(sql).hasSize(2);
if (isH2()) {
assertThat(sql.get(0)).contains("select t0.id, t0.name, t0.version, t0.ma_street, t0.ma_suburb, t0.ma_city, t0.ma_country_code from eper_addr t0 where t0.id = ? ");
assertThat(sql.get(1)).contains("select t0.code, t0.name from o_country t0 where t0.code = ? ; --bind(NZ,");
}
}
}