mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-19 23:02:24 +00:00
Refactor move tests from ebean-core to ebean-test
This commit is contained in:
1 parent
0b00a14fd3
commit
eab56d323b
1650 files changed
+185
-147
No files matched your search
@@ -0,0 +1,47 @@
|
||||
package org.tests.inheritance;
|
||||
|
||||
import javax.persistence.DiscriminatorValue;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToOne;
|
||||
|
||||
/**
|
||||
*/
|
||||
@Entity
|
||||
public class InnerReport {
|
||||
|
||||
@Id
|
||||
Long id;
|
||||
|
||||
String name;
|
||||
|
||||
@OneToOne
|
||||
Forecast forecast;
|
||||
|
||||
public Forecast getForecast() {
|
||||
return forecast;
|
||||
}
|
||||
|
||||
public void setForecast(Forecast forecast) {
|
||||
this.forecast = forecast;
|
||||
}
|
||||
|
||||
|
||||
@Entity
|
||||
@DiscriminatorValue("F")
|
||||
public static class Forecast extends Stockforecast {
|
||||
|
||||
@ManyToOne
|
||||
InnerReport innerReport;
|
||||
|
||||
public InnerReport getInnerReport() {
|
||||
return innerReport;
|
||||
}
|
||||
|
||||
public void setInnerReport(InnerReport innerReport) {
|
||||
this.innerReport = innerReport;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package org.tests.inheritance;
|
||||
|
||||
import javax.persistence.DiscriminatorColumn;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Inheritance;
|
||||
import javax.persistence.InheritanceType;
|
||||
import javax.persistence.MappedSuperclass;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import io.ebean.annotation.Index;
|
||||
|
||||
/**
|
||||
* Model class to reference an organization node.
|
||||
*
|
||||
* @author Christian Hartl, FOCONIS AG
|
||||
*/
|
||||
@Entity
|
||||
@MappedSuperclass
|
||||
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
|
||||
@DiscriminatorColumn(name = "kind")
|
||||
@Index(unique = false, columnNames = "kind")
|
||||
public abstract class OrganizationNode {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@OneToOne(cascade = {})
|
||||
@NotNull
|
||||
private OrganizationTreeNode parentTreeNode;
|
||||
|
||||
public OrganizationTreeNode getParentTreeNode() {
|
||||
return parentTreeNode;
|
||||
}
|
||||
|
||||
public void setParentTreeNode(OrganizationTreeNode parentTreeNode) {
|
||||
this.parentTreeNode = parentTreeNode;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package org.tests.inheritance;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import static javax.persistence.CascadeType.ALL;
|
||||
import static javax.persistence.FetchType.LAZY;
|
||||
|
||||
/**
|
||||
* Model class to reference an organization tree node.
|
||||
*
|
||||
* @author Christian Hartl, FOCONIS AG
|
||||
*/
|
||||
@Entity
|
||||
public class OrganizationTreeNode {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@OneToOne(cascade = ALL, fetch = LAZY, mappedBy = "parentTreeNode", orphanRemoval = true)
|
||||
@NotNull
|
||||
private OrganizationNode organizationNode;
|
||||
|
||||
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 OrganizationNode getOrganizationNode() {
|
||||
return organizationNode;
|
||||
}
|
||||
|
||||
public void setOrganizationNode(OrganizationNode organizationNode) {
|
||||
this.organizationNode = organizationNode;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.tests.inheritance;
|
||||
|
||||
import javax.persistence.DiscriminatorValue;
|
||||
import javax.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
@DiscriminatorValue("Unit")
|
||||
public class OrganizationUnit extends OrganizationNode {
|
||||
|
||||
private String title;
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package org.tests.inheritance;
|
||||
|
||||
import javax.persistence.DiscriminatorColumn;
|
||||
import javax.persistence.DiscriminatorType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Inheritance;
|
||||
import javax.persistence.InheritanceType;
|
||||
|
||||
|
||||
@Entity
|
||||
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
|
||||
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
|
||||
public abstract class Stockforecast {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package org.tests.inheritance;
|
||||
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.AttributeHolder;
|
||||
import org.tests.model.basic.ListAttribute;
|
||||
import org.tests.model.basic.ListAttributeValue;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class TestDuplicateKeyException extends BaseTestCase {
|
||||
|
||||
/**
|
||||
* Test query.
|
||||
* <p>This test covers the BUG 276. Cascade was not propagating to the ListAttribute because
|
||||
* it was considered safe to skip as it didn't take into account any derived classes
|
||||
* into account with e.g. collections and Cascade options </p>
|
||||
*/
|
||||
@Test
|
||||
public void testQuery() {
|
||||
|
||||
// Setup the data first
|
||||
final ListAttributeValue value1 = new ListAttributeValue();
|
||||
|
||||
DB.save(value1);
|
||||
|
||||
final ListAttribute listAttribute = new ListAttribute();
|
||||
listAttribute.add(value1);
|
||||
DB.save(listAttribute);
|
||||
|
||||
|
||||
final AttributeHolder holder = new AttributeHolder();
|
||||
holder.add(listAttribute);
|
||||
|
||||
try {
|
||||
DB.execute(() -> {
|
||||
//DB.currentTransaction().log("-- saving holder first time");
|
||||
// Alternatively turn off cascade Persist for this transaction
|
||||
//DB.currentTransaction().setPersistCascade(false);
|
||||
DB.save(holder);
|
||||
//DB.currentTransaction().log("-- saving holder second time");
|
||||
// we don't get this far before failing
|
||||
//DB.save(holder);
|
||||
});
|
||||
} catch (Exception e) {
|
||||
assertEquals(e.getMessage(), "test rollback");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
package org.tests.inheritance;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import org.tests.model.basic.Car;
|
||||
import org.tests.model.basic.CarAccessory;
|
||||
import org.tests.model.basic.CarFuse;
|
||||
import org.tests.model.basic.Truck;
|
||||
import org.tests.model.basic.Vehicle;
|
||||
import org.tests.model.basic.VehicleDriver;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestInheritInsert extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testCasting() {
|
||||
|
||||
Truck t = new Truck();
|
||||
t.setCapacity(10d);
|
||||
DB.save(t);
|
||||
|
||||
Vehicle v = DB.find(Vehicle.class, t.getId());
|
||||
if (v instanceof Truck) {
|
||||
Truck t0 = (Truck) v;
|
||||
assertEquals(Double.valueOf(10d), t0.getCapacity());
|
||||
assertEquals(Double.valueOf(10d), ((Truck) v).getCapacity());
|
||||
assertNotNull(t0.getId());
|
||||
} else {
|
||||
fail();
|
||||
}
|
||||
|
||||
VehicleDriver driver = new VehicleDriver();
|
||||
driver.setName("Jim");
|
||||
driver.setVehicle(v);
|
||||
|
||||
DB.save(driver);
|
||||
|
||||
VehicleDriver d1 = DB.find(VehicleDriver.class, driver.getId());
|
||||
v = d1.getVehicle();
|
||||
if (v instanceof Truck) {
|
||||
Double capacity = ((Truck) v).getCapacity();
|
||||
assertEquals(Double.valueOf(10d), capacity);
|
||||
assertNotNull(v.getId());
|
||||
} else {
|
||||
fail("v not a Truck?");
|
||||
}
|
||||
|
||||
List<VehicleDriver> list = DB.find(VehicleDriver.class).findList();
|
||||
for (VehicleDriver vehicleDriver : list) {
|
||||
if (vehicleDriver.getVehicle() instanceof Truck) {
|
||||
Double capacity = ((Truck) vehicleDriver.getVehicle()).getCapacity();
|
||||
assertEquals(Double.valueOf(10d), capacity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQuery() {
|
||||
|
||||
Car car = new Car();
|
||||
car.setLicenseNumber("MARIOS_CAR_LICENSE");
|
||||
DB.save(car);
|
||||
|
||||
VehicleDriver driver = new VehicleDriver();
|
||||
driver.setName("Mario");
|
||||
driver.setVehicle(car);
|
||||
DB.save(driver);
|
||||
|
||||
Query<VehicleDriver> query = DB.find(VehicleDriver.class);
|
||||
query.where().eq("vehicle.licenseNumber", "MARIOS_CAR_LICENSE");
|
||||
List<VehicleDriver> drivers = query.findList();
|
||||
|
||||
assertNotNull(drivers);
|
||||
assertEquals(1, drivers.size());
|
||||
assertNotNull(drivers.get(0));
|
||||
|
||||
assertEquals("Mario", drivers.get(0).getName());
|
||||
assertEquals("MARIOS_CAR_LICENSE", drivers.get(0).getVehicle().getLicenseNumber());
|
||||
|
||||
Vehicle car2 = DB.find(Vehicle.class, car.getId());
|
||||
|
||||
car2.setLicenseNumber("test");
|
||||
DB.save(car);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_AtOrderBy_on_ChildOfChild() {
|
||||
|
||||
Car car = new Car();
|
||||
car.setLicenseNumber("ABC");
|
||||
DB.save(car);
|
||||
|
||||
CarFuse fuse = new CarFuse();
|
||||
fuse.setLocationCode("xdfg");
|
||||
DB.save(fuse);
|
||||
|
||||
CarAccessory accessory = new CarAccessory(car, fuse);
|
||||
DB.save(accessory);
|
||||
|
||||
|
||||
Query<Car> query = DB.find(Car.class)
|
||||
.fetch("accessories")
|
||||
.where()
|
||||
.eq("id", car.getId())
|
||||
.query();
|
||||
|
||||
Car result = query.findOne();
|
||||
|
||||
assertSql(query).contains("order by t0.id, t2.location_code");
|
||||
assertSql(query).contains("left join car_fuse t2 on t2.id = t1.fuse_id");
|
||||
|
||||
assertNotNull(result);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package org.tests.inheritance;
|
||||
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.Car;
|
||||
import org.tests.model.basic.Truck;
|
||||
import org.tests.model.basic.Vehicle;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class TestInheritanceBatchLazyLoad {
|
||||
|
||||
@Test
|
||||
public void lazyLoadProperty_when_propertyNotOnAllInheritanceTypes() {
|
||||
|
||||
Car c = new Car();
|
||||
c.setLicenseNumber("VZVZ1");
|
||||
c.setDriver("CarDriver");
|
||||
DB.save(c);
|
||||
|
||||
Truck t = new Truck();
|
||||
t.setLicenseNumber("VZVZ2");
|
||||
t.setCapacity(20D);
|
||||
DB.save(t);
|
||||
|
||||
|
||||
List<Vehicle> list = DB.find(Vehicle.class)
|
||||
.select("licenseNumber")
|
||||
.where().startsWith("licenseNumber", "VZVZ")
|
||||
.order().asc("licenseNumber")
|
||||
.findList();
|
||||
|
||||
assertThat(list).hasSize(2);
|
||||
|
||||
Car car = (Car) list.get(0);
|
||||
car.getNotes();
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package org.tests.inheritance;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.inheritance.model.GroupConfiguration;
|
||||
import org.tests.inheritance.model.ProductConfiguration;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TestInheritanceDiscriminatorQueryCache extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testDiscriminatorQueryCache() {
|
||||
Database server = DB.getDefault();
|
||||
|
||||
ProductConfiguration pc1 = new ProductConfiguration();
|
||||
pc1.setName("PC1");
|
||||
server.save(pc1);
|
||||
|
||||
ProductConfiguration pc2 = new ProductConfiguration();
|
||||
pc1.setName("PC2");
|
||||
server.save(pc2);
|
||||
|
||||
GroupConfiguration gc1 = new GroupConfiguration();
|
||||
gc1.setName("GC1");
|
||||
server.save(gc1);
|
||||
|
||||
GroupConfiguration gc2 = new GroupConfiguration();
|
||||
gc1.setName("GC2");
|
||||
server.save(gc2);
|
||||
|
||||
List<ProductConfiguration> list1 = server.createQuery(ProductConfiguration.class).setUseQueryCache(true).findList();
|
||||
List<GroupConfiguration> list2 = server.createQuery(GroupConfiguration.class).setUseQueryCache(true).findList();
|
||||
|
||||
for(ProductConfiguration pc : list1) {
|
||||
System.out.print(pc.getProductName());
|
||||
}
|
||||
for(GroupConfiguration gc : list2) {
|
||||
System.out.print(gc.getGroupName());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
package org.tests.inheritance;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.inheritance.model.CalculationResult;
|
||||
import org.tests.inheritance.model.Configurations;
|
||||
import org.tests.inheritance.model.GroupConfiguration;
|
||||
import org.tests.inheritance.model.ProductConfiguration;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestInheritanceJoins extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testAssocOne() {
|
||||
|
||||
Database server = DB.getDefault();
|
||||
|
||||
ProductConfiguration pc = new ProductConfiguration();
|
||||
pc.setName("PC1");
|
||||
server.save(pc);
|
||||
|
||||
GroupConfiguration gc = new GroupConfiguration();
|
||||
gc.setName("GC1");
|
||||
server.save(gc);
|
||||
|
||||
CalculationResult r = new CalculationResult();
|
||||
r.setCharge(100.0);
|
||||
r.setProductConfiguration(pc);
|
||||
r.setGroupConfiguration(gc);
|
||||
server.save(r);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assocOne_when_null() {
|
||||
|
||||
Database server = DB.getDefault();
|
||||
|
||||
GroupConfiguration gc = new GroupConfiguration();
|
||||
gc.setName("GC1");
|
||||
server.save(gc);
|
||||
|
||||
CalculationResult r = new CalculationResult();
|
||||
r.setCharge(100.0);
|
||||
|
||||
// @ManyToOne with inheritance and null
|
||||
r.setProductConfiguration(null);
|
||||
r.setGroupConfiguration(gc);
|
||||
server.save(r);
|
||||
|
||||
CalculationResult result = server.find(CalculationResult.class, r.getId());
|
||||
|
||||
GroupConfiguration group = result.getGroupConfiguration();
|
||||
assertEquals(group.getId(), gc.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAssocOneWithNullAssoc() {
|
||||
|
||||
/* Ensures the fetch join to a property with inheritance work as a left join */
|
||||
|
||||
Database server = DB.getDefault();
|
||||
|
||||
final ProductConfiguration pc = new ProductConfiguration();
|
||||
pc.setName("PC1");
|
||||
server.save(pc);
|
||||
|
||||
CalculationResult r = new CalculationResult();
|
||||
final Double charge = 100.0;
|
||||
r.setCharge(charge);
|
||||
r.setProductConfiguration(pc);
|
||||
r.setGroupConfiguration(null);
|
||||
server.save(r);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAssocMany() {
|
||||
Configurations configurations = new Configurations();
|
||||
|
||||
Database server = DB.getDefault();
|
||||
|
||||
server.save(configurations);
|
||||
|
||||
|
||||
final GroupConfiguration gc = new GroupConfiguration("GC1");
|
||||
configurations.addGroupConfiguration(gc);
|
||||
|
||||
|
||||
server.save(gc);
|
||||
|
||||
|
||||
Configurations configurationsQueried = server.find(Configurations.class, configurations.getId());
|
||||
|
||||
List<GroupConfiguration> groups = configurationsQueried.getGroupConfigurations();
|
||||
|
||||
assertTrue(!groups.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAssocManyWithNoneRelated() {
|
||||
Configurations configurations = new Configurations();
|
||||
|
||||
Database server = DB.getDefault();
|
||||
|
||||
server.save(configurations);
|
||||
|
||||
Configurations configurationsQueried = server.find(Configurations.class).fetch("groupConfigurations").where().idEq(configurations.getId()).findOne();
|
||||
|
||||
assertNotNull(configurationsQueried);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultipleAssocManyWithSameInheritanceBase() {
|
||||
Database database = DB.getDefault();
|
||||
|
||||
Configurations configurations = new Configurations();
|
||||
database.save(configurations);
|
||||
|
||||
GroupConfiguration gc = new GroupConfiguration();
|
||||
gc.setConfigurations(configurations);
|
||||
database.save(gc);
|
||||
|
||||
ProductConfiguration pc = new ProductConfiguration();
|
||||
pc.setConfigurations(configurations);
|
||||
database.save(pc);
|
||||
|
||||
Configurations configurationsQueried = database.find(Configurations.class).fetch("groupConfigurations").fetch("productConfigurations").where().idEq(configurations.getId()).findOne();
|
||||
|
||||
assertEquals(Collections.singletonList(gc), configurationsQueried.getGroupConfigurations());
|
||||
assertEquals(Collections.singletonList(pc), configurationsQueried.getProductConfigurations());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
package org.tests.inheritance;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.bean.BeanCollection.ModifyListenMode;
|
||||
import io.ebean.common.BeanList;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Animal;
|
||||
import org.tests.model.basic.AnimalShelter;
|
||||
import org.tests.model.basic.BigDog;
|
||||
import org.tests.model.basic.Cat;
|
||||
import org.tests.model.basic.Dog;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class TestInheritanceOnMany extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
Cat cat = new Cat();
|
||||
cat.setName("Puss");
|
||||
DB.save(cat);
|
||||
|
||||
Dog dog = new Dog();
|
||||
dog.setRegistrationNumber("DOGGIE");
|
||||
DB.save(dog);
|
||||
|
||||
BigDog bd = new BigDog();
|
||||
bd.setDogSize("large");
|
||||
bd.setRegistrationNumber("BG1");
|
||||
DB.save(bd);
|
||||
|
||||
AnimalShelter shelter = new AnimalShelter();
|
||||
shelter.setName("My Animal Shelter");
|
||||
shelter.getAnimals().add(cat);
|
||||
shelter.getAnimals().add(dog);
|
||||
|
||||
DB.save(shelter);
|
||||
|
||||
AnimalShelter shelter2 = DB.find(AnimalShelter.class, shelter.getId());
|
||||
List<Animal> animals = shelter2.getAnimals();
|
||||
|
||||
BeanList<?> beanList = (BeanList<?>) animals;
|
||||
ModifyListenMode modifyListenMode = beanList.getModifyListenMode();
|
||||
|
||||
assertNotNull(modifyListenMode);
|
||||
|
||||
assertNotNull(DB.find(Animal.class).findList());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetReferenceOnConcrete() {
|
||||
|
||||
Dog dog = new Dog();
|
||||
dog.setRegistrationNumber("D2");
|
||||
DB.save(dog);
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
// Dog is concrete so we return as Dog even though
|
||||
// it could be a BigDog (so we are trusting the caller)
|
||||
Dog ref = DB.reference(Dog.class, dog.getId());
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).isEmpty();
|
||||
assertNotNull(ref);
|
||||
|
||||
// invoke lazy loading
|
||||
assertEquals("D2", ref.getRegistrationNumber());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetReferenceOnLeaf() {
|
||||
|
||||
BigDog bd = new BigDog();
|
||||
bd.setDogSize("large");
|
||||
bd.setRegistrationNumber("BG2");
|
||||
DB.save(bd);
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
BigDog bigDog = DB.reference(BigDog.class, bd.getId());
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).isEmpty();
|
||||
assertNotNull(bigDog);
|
||||
|
||||
// invoke lazy loading
|
||||
assertEquals("BG2", bigDog.getRegistrationNumber());
|
||||
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
// Animal is abstract so we hit the DB
|
||||
Animal animal = DB.reference(Animal.class, bd.getId());
|
||||
|
||||
sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(1);
|
||||
assertThat(trimSql(sql.get(0), 2)).contains("select t0.species, t0.id from animal t0 where t0.id = ?");
|
||||
assertNotNull(animal);
|
||||
|
||||
// invoke lazy loading
|
||||
assertNotNull(animal.getVersion());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package org.tests.inheritance;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebeantest.LoggedSql;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.inheritance.order.OrderMasterInheritance;
|
||||
import org.tests.inheritance.order.OrderedA;
|
||||
import org.tests.inheritance.order.OrderedB;
|
||||
import org.tests.inheritance.order.OrderedParent;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestInheritanceOrderColumn extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
final OrderMasterInheritance master = new OrderMasterInheritance();
|
||||
final OrderedA orderedA = new OrderedA();
|
||||
orderedA.setCommonName("commonOrderedA");
|
||||
orderedA.setOrderedAName("orderedA");
|
||||
|
||||
final OrderedB orderedB = new OrderedB();
|
||||
orderedB.setCommonName("commonOrderedB");
|
||||
orderedB.setOrderedBName("orderedB");
|
||||
|
||||
master.getReferenced().add(orderedA);
|
||||
master.getReferenced().add(orderedB);
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
DB.save(master);
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(5);
|
||||
// Some platforms insert ids and others don't need to...
|
||||
assertSql(trimId(sql, 1))
|
||||
.contains("insert into ordered_parent (order_master_inheritance_id, dtype, common_name, ordered_aname, sort_order) values");
|
||||
assertSql(trimId(sql, 3))
|
||||
.contains("insert into ordered_parent (order_master_inheritance_id, dtype, common_name, ordered_bname, sort_order) values");
|
||||
|
||||
OrderMasterInheritance result = DB.find(OrderMasterInheritance.class).findOne();
|
||||
assertThat(result.getReferenced())
|
||||
.extracting(OrderedParent::getCommonName)
|
||||
.containsExactly("commonOrderedA", "commonOrderedB");
|
||||
|
||||
// Swap the two
|
||||
result.getReferenced().add(0, result.getReferenced().remove(1));
|
||||
assertThat(result.getReferenced())
|
||||
.extracting(OrderedParent::getCommonName)
|
||||
.containsExactly("commonOrderedB", "commonOrderedA");
|
||||
|
||||
LoggedSql.start();
|
||||
DB.save(result);
|
||||
sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(3);
|
||||
assertThat(sql.get(0)).contains("update ordered_parent set sort_order=? where id=?");
|
||||
|
||||
result = DB.find(OrderMasterInheritance.class).findOne();
|
||||
assertThat(result.getReferenced())
|
||||
.extracting(OrderedParent::getCommonName)
|
||||
.containsExactly("commonOrderedB", "commonOrderedA");
|
||||
}
|
||||
|
||||
private String trimId(List<String> sql, int i) {
|
||||
return sql.get(i).replace("(id, ", "(");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package org.tests.inheritance;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.inherit.ChildA;
|
||||
import org.tests.inherit.ChildB;
|
||||
import org.tests.inherit.Parent;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestInheritanceQuery extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
Parent a = new ChildA(90, "Bean A");
|
||||
Parent b1 = new ChildB(91, "Bean B1");
|
||||
Parent b2 = new ChildB(92, "Bean B2");
|
||||
|
||||
server().save(a);
|
||||
server().save(b1);
|
||||
server().save(b2);
|
||||
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
Query<Parent> query = DB.find(Parent.class);
|
||||
|
||||
query.where().in("val", 90, 91); // restrict to a & b1
|
||||
|
||||
assertThat(query.findList()).hasSize(2); // a & b1
|
||||
|
||||
Query<Parent> query2 = query.copy();
|
||||
query2.setInheritType(ChildA.class);
|
||||
|
||||
assertThat(query2.findList()).containsExactly(a);
|
||||
|
||||
query2.setInheritType(ChildB.class);
|
||||
assertThat(query2.findList()).containsExactly(b1);
|
||||
|
||||
query2 = DB.find(Parent.class).setInheritType(ChildB.class);
|
||||
assertThat(query2.findList()).contains(b1, b2);
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(4);
|
||||
assertSql(sql.get(1)).contains("where t0.type = 'A'");
|
||||
assertSql(sql.get(2)).contains("where t0.type = 'B'");
|
||||
assertThat(sql.get(3)).contains("where t0.type = 'B'");
|
||||
|
||||
DB.delete(a);
|
||||
DB.delete(b1);
|
||||
DB.delete(b2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package org.tests.inheritance;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.RawSql;
|
||||
import io.ebean.RawSqlBuilder;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Truck;
|
||||
import org.tests.model.basic.Vehicle;
|
||||
import org.tests.model.basic.VehicleLeaseLong;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestInheritanceRawSql extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
Truck truck = new Truck();
|
||||
truck.setCapacity(50D);
|
||||
truck.setLicenseNumber("ASB23");
|
||||
|
||||
DB.save(truck);
|
||||
|
||||
|
||||
String sql = "select dtype, id, license_number from vehicle where id = :id";
|
||||
RawSqlBuilder rawSqlBuilder = RawSqlBuilder.parse(sql);
|
||||
|
||||
RawSql rawSql = rawSqlBuilder.create();
|
||||
|
||||
List<Vehicle> list = DB.find(Vehicle.class)
|
||||
.setRawSql(rawSql)
|
||||
.setParameter("id", truck.getId())
|
||||
.findList();
|
||||
|
||||
assertEquals(1, list.size());
|
||||
|
||||
Vehicle vehicle2 = list.get(0);
|
||||
assertTrue(vehicle2 instanceof Truck);
|
||||
|
||||
Truck truck2 = (Truck) vehicle2;
|
||||
assertEquals("ASB23", truck2.getLicenseNumber());
|
||||
|
||||
// invoke lazy loading and set the capacity
|
||||
truck2.setCapacity(30D);
|
||||
|
||||
// and now save
|
||||
DB.save(truck2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinToInheritance() {
|
||||
|
||||
VehicleLeaseLong longLease = new VehicleLeaseLong();
|
||||
longLease.setName("Long one");
|
||||
longLease.setBond(new BigDecimal("1000"));
|
||||
longLease.setMinDuration(100);
|
||||
longLease.setActiveEnd(LocalDate.now());
|
||||
|
||||
DB.save(longLease);
|
||||
|
||||
|
||||
Truck truck = new Truck();
|
||||
truck.setCapacity(100D);
|
||||
truck.setLicenseNumber("ZK1");
|
||||
truck.setLease(longLease);
|
||||
|
||||
DB.save(truck);
|
||||
|
||||
|
||||
String sql = "select v.dtype, v.id, v.license_number, l.dtype, l.id, l.name " +
|
||||
"from vehicle v " +
|
||||
"join vehicle_lease l on l.id = v.lease_id " +
|
||||
"where v.id = ?";
|
||||
|
||||
RawSql rawSql = RawSqlBuilder.parse(sql)
|
||||
.tableAliasMapping("v", null)
|
||||
.tableAliasMapping("l", "lease")
|
||||
.create();
|
||||
|
||||
Vehicle veh = DB.find(Vehicle.class)
|
||||
.setRawSql(rawSql)
|
||||
.setParameter(truck.getId())
|
||||
.findOne();
|
||||
|
||||
assertThat(veh).isNotNull();
|
||||
assertThat(veh.getLicenseNumber()).isEqualTo("ZK1");
|
||||
assertThat(veh.getLease().getName()).isEqualTo("Long one");
|
||||
|
||||
|
||||
RawSql rawSql2 = RawSqlBuilder.parse(sql)
|
||||
.columnMappingIgnore("v.dtype")
|
||||
.columnMapping("v.id", "id")
|
||||
.columnMapping("v.license_number", "licenseNumber")
|
||||
.columnMappingIgnore("l.dtype")
|
||||
.columnMapping("l.id", "lease.id")
|
||||
.columnMapping("l.name", "lease.name")
|
||||
.create();
|
||||
|
||||
Vehicle veh2 = DB.find(Vehicle.class)
|
||||
.setRawSql(rawSql2)
|
||||
.setParameter(truck.getId())
|
||||
.findOne();
|
||||
|
||||
assertThat(veh2).isNotNull();
|
||||
assertThat(veh2.getLicenseNumber()).isEqualTo("ZK1");
|
||||
assertThat(veh2.getLease().getName()).isEqualTo("Long one");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package org.tests.inheritance;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.tests.inherit.ChildA;
|
||||
import org.tests.inherit.ChildB;
|
||||
import org.tests.inherit.Parent;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
|
||||
public class TestInheritanceRefBean extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
Parent a = new ChildA(42, "Bean A");
|
||||
Parent b = new ChildB(43, "Bean B");
|
||||
|
||||
server().save(a);
|
||||
server().save(b);
|
||||
Long idA = a.getId();
|
||||
Long idB = b.getId();
|
||||
|
||||
Parent test;
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
test = server().reference(ChildA.class, idA);
|
||||
assertTrue(test instanceof ChildA);
|
||||
assertEquals(0, LoggedSqlCollector.stop().size());
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
test = server().reference(Parent.class, idB);
|
||||
assertTrue(test instanceof ChildB);
|
||||
assertEquals(1, LoggedSqlCollector.stop().size());
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package org.tests.inheritance;
|
||||
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.TIntChild;
|
||||
import org.tests.model.basic.TIntRoot;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestIntInherit extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testMe() {
|
||||
|
||||
TIntRoot r = new TIntRoot();
|
||||
r.setName("root1");
|
||||
|
||||
TIntRoot r2 = new TIntRoot();
|
||||
r.setName("root2");
|
||||
|
||||
TIntChild c1 = new TIntChild();
|
||||
c1.setName("child1");
|
||||
c1.setChildProperty("cp1");
|
||||
|
||||
TIntChild c2 = new TIntChild();
|
||||
c2.setName("child2");
|
||||
c2.setChildProperty("cp2");
|
||||
|
||||
|
||||
DB.save(r);
|
||||
DB.save(r2);
|
||||
DB.save(c1);
|
||||
DB.save(c2);
|
||||
|
||||
TIntRoot result1 = DB.find(TIntRoot.class, r.getId());
|
||||
assertTrue(result1 instanceof TIntRoot);
|
||||
|
||||
TIntRoot ref3 = DB.reference(TIntRoot.class, c1.getId());
|
||||
assertTrue(ref3 instanceof TIntRoot);
|
||||
|
||||
TIntRoot result3 = DB.find(TIntRoot.class, c1.getId());
|
||||
assertTrue(result3 instanceof TIntChild);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.tests.inheritance;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TestNpeOnDiscriminator extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
InnerReport report = DB.json().toBean(InnerReport.class, "{}");
|
||||
DB.save(report);
|
||||
|
||||
// other service ...
|
||||
InnerReport.Forecast f = new InnerReport.Forecast();
|
||||
report.setForecast(f);
|
||||
f.innerReport = report;
|
||||
|
||||
DB.save(f);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package org.tests.inheritance;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.AttributeHolder;
|
||||
import org.tests.model.basic.ListAttribute;
|
||||
import org.tests.model.basic.ListAttributeValue;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestSkippable extends BaseTestCase {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(TestSkippable.class);
|
||||
|
||||
/**
|
||||
* Test query.
|
||||
* <p>This test covers the BUG 276. Cascade was not propagating to the ListAttribute because
|
||||
* it was considered safe to skip as it didn't take into account any derived classes
|
||||
* into account with e.g. collections and Cascade options </p>
|
||||
*/
|
||||
@Test
|
||||
public void testQuery() {
|
||||
|
||||
// Setup the data first
|
||||
final ListAttributeValue value1 = new ListAttributeValue();
|
||||
final ListAttributeValue value2 = new ListAttributeValue();
|
||||
|
||||
DB.save(value1);
|
||||
DB.save(value2);
|
||||
|
||||
final ListAttribute listAttribute = new ListAttribute();
|
||||
listAttribute.add(value1);
|
||||
DB.save(listAttribute);
|
||||
logger.info(" -- seeded data");
|
||||
|
||||
final ListAttribute listAttributeDB = DB.find(ListAttribute.class, listAttribute.getId());
|
||||
assertNotNull(listAttributeDB);
|
||||
|
||||
final ListAttributeValue value1_DB = listAttributeDB.getValues().iterator().next();
|
||||
assertTrue(value1.getId().equals(value1_DB.getId()));
|
||||
logger.info(" -- asserted data in db");
|
||||
|
||||
|
||||
final AttributeHolder holder = new AttributeHolder();
|
||||
holder.add(listAttributeDB);
|
||||
|
||||
DB.save(holder);
|
||||
logger.info(" -- saved holder");
|
||||
|
||||
// Now change the M2M listAttribute.values and save the holder
|
||||
// The save should cascade as follows
|
||||
// holder.attributes..ListAttribute.values
|
||||
listAttributeDB.getValues().clear();
|
||||
listAttributeDB.add(value2);
|
||||
|
||||
// Save the holder - should cascade down to the listAtribute and save the values
|
||||
DB.save(holder);
|
||||
logger.info(" -- M2M detected delete of value1 and add of value2 ?");
|
||||
|
||||
|
||||
final ListAttribute listAttributeDB_2 = DB.find(ListAttribute.class, listAttributeDB.getId());
|
||||
assertNotNull(listAttributeDB_2);
|
||||
final ListAttributeValue value2_DB_2 = listAttributeDB_2.getValues().iterator().next();
|
||||
|
||||
|
||||
assertEquals(value2.getId(), value2_DB_2.getId());
|
||||
assertTrue(value2.getId().equals(value2_DB_2.getId()));
|
||||
|
||||
DB.delete(listAttributeDB_2);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package org.tests.inheritance;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TestTreeOrganisations extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
OrganizationTreeNode treeNode = new OrganizationTreeNode();
|
||||
treeNode.setName("tree");
|
||||
|
||||
OrganizationUnit node = new OrganizationUnit();
|
||||
node.setTitle("node");
|
||||
|
||||
treeNode.setOrganizationNode(node);
|
||||
DB.save(treeNode);
|
||||
|
||||
treeNode = DB.find(OrganizationTreeNode.class, treeNode.getId());
|
||||
assertEquals(node, treeNode.getOrganizationNode());
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package org.tests.inheritance.abstrakt;
|
||||
|
||||
import javax.persistence.DiscriminatorColumn;
|
||||
import javax.persistence.DiscriminatorType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Inheritance;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Version;
|
||||
|
||||
@Entity
|
||||
@Table(name = "block")
|
||||
@Inheritance
|
||||
@DiscriminatorColumn(name = "case_type", discriminatorType = DiscriminatorType.INTEGER)
|
||||
public abstract class AbstractBaseBlock {
|
||||
|
||||
@Id
|
||||
long id;
|
||||
|
||||
String name;
|
||||
|
||||
@Version
|
||||
long version;
|
||||
|
||||
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 long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.tests.inheritance.abstrakt;
|
||||
|
||||
import javax.persistence.DiscriminatorValue;
|
||||
import javax.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
@DiscriminatorValue(value = "2")
|
||||
public class Block extends AbstractBaseBlock {
|
||||
|
||||
String notes;
|
||||
|
||||
public String getNotes() {
|
||||
return notes;
|
||||
}
|
||||
|
||||
public void setNotes(String notes) {
|
||||
this.notes = notes;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package org.tests.inheritance.abstrakt;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TestInheritanceNum extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void basicIUD() {
|
||||
|
||||
Block block = new Block();
|
||||
block.setName("ibe");
|
||||
block.setNotes("try it");
|
||||
|
||||
DB.save(block);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package org.tests.inheritance.bothsides;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
public class SourceA extends SourceBase {
|
||||
|
||||
@ManyToOne(cascade = CascadeType.ALL)
|
||||
private Target1 target;
|
||||
|
||||
public SourceA(String name, Target1 target, int pos) {
|
||||
super(name, pos);
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
public Target1 getTarget() {
|
||||
return target;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.tests.inheritance.bothsides;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
public class SourceB extends SourceBase {
|
||||
|
||||
@ManyToOne(cascade = CascadeType.PERSIST)
|
||||
private Target2 target;
|
||||
|
||||
public SourceB(String name, Target2 target, int pos) {
|
||||
super(name, pos);
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
public Target2 getTarget() { return target;}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package org.tests.inheritance.bothsides;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Inheritance;
|
||||
|
||||
@Entity
|
||||
@Inheritance
|
||||
public abstract class SourceBase extends WithAutoGeneratedUUID {
|
||||
|
||||
private String name;
|
||||
|
||||
private int pos;
|
||||
|
||||
public SourceBase(String name, int pos) {
|
||||
this.name = name;
|
||||
this.pos = pos;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getPos() {
|
||||
return pos;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.tests.inheritance.bothsides;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Inheritance;
|
||||
|
||||
@Entity
|
||||
@Inheritance
|
||||
public class Target1 extends TargetBase {
|
||||
|
||||
public Target1(String name) {
|
||||
super(name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package org.tests.inheritance.bothsides;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
public class Target2 extends TargetBase {
|
||||
public Target2(String name) {
|
||||
super(name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package org.tests.inheritance.bothsides;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Inheritance;
|
||||
|
||||
@Entity
|
||||
@Inheritance
|
||||
public abstract class TargetBase extends WithAutoGeneratedUUID {
|
||||
|
||||
private String name;
|
||||
|
||||
public TargetBase(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() { return name;}
|
||||
|
||||
}
|
||||
+150
@@ -0,0 +1,150 @@
|
||||
package org.tests.inheritance.bothsides;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.Query;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestInheritanceBothSides extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void selectSourceBaseSql() {
|
||||
|
||||
final Query<SourceBase> query = DB.find(SourceBase.class).order("pos");
|
||||
query.findList();
|
||||
|
||||
assertThat(sqlOf(query)).contains("select t0.dtype, t0.id, t0.name, t0.pos, t1.dtype, t0.target_id, t1.dtype, t0.target_id from source_base t0 left join target_base t1 on t1.id = t0.target_id order by t0.pos");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void selectSourceASql() {
|
||||
|
||||
final Query<SourceA> query = DB.find(SourceA.class).order("pos");
|
||||
query.findList();
|
||||
|
||||
assertThat(sqlOf(query)).contains("select t0.dtype, t0.id, t0.name, t0.pos, t1.dtype, t0.target_id from source_base t0 left join target_base t1 on t1.id = t0.target_id where t0.dtype = 'SourceA' order by t0.pos");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void selectSourceAWithJoin() {
|
||||
|
||||
final Query<SourceA> query = DB.find(SourceA.class).fetch("target", "name").order("pos");
|
||||
query.findList();
|
||||
|
||||
assertThat(sqlOf(query)).contains("select t0.dtype, t0.id, t0.name, t0.pos, t1.dtype, t1.id, t1.name from source_base t0 left join target_base t1 on t1.id = t0.target_id and t1.dtype = 'Target1' where t0.dtype = 'SourceA' order by t0.pos");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
Target1 target1 = new Target1("target 1");
|
||||
Target2 target2 = new Target2("target 2");
|
||||
Target1 target1b = new Target1("target 1b");
|
||||
|
||||
SourceA sourceA = new SourceA("source a", target1, 1);
|
||||
SourceB sourceB = new SourceB("source b", target2, 2);
|
||||
SourceA sourceA2 = new SourceA("source a2", target1b, 3);
|
||||
|
||||
DB.saveAll(asList(sourceA, sourceB, sourceA2));
|
||||
|
||||
final Database db = DB.getDefault();
|
||||
|
||||
final SourceBase foundA = DB.find(SourceBase.class, db.beanId(sourceA));
|
||||
final SourceBase foundB = DB.find(SourceBase.class, db.beanId(sourceB));
|
||||
|
||||
assertSourceBaseEqual(foundA, sourceA);
|
||||
assertThat(foundA).isInstanceOf(SourceA.class);
|
||||
assertSourceAEqual((SourceA) foundA, sourceA);
|
||||
|
||||
assertSourceBaseEqual(foundB, sourceB);
|
||||
assertThat(foundB).isInstanceOf(SourceB.class);
|
||||
assertSourceBEqual((SourceB) foundB, sourceB);
|
||||
|
||||
assertFetchAllDoubleLazyLoading();
|
||||
assertFetchAllSourceAs();
|
||||
}
|
||||
|
||||
private void assertFetchAllSourceAs() {
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
final List<SourceA> sourceAList = DB.find(SourceA.class)
|
||||
.fetch("target", "name")
|
||||
.order("pos")
|
||||
.findList();
|
||||
|
||||
final String joinedNames = sourceAList.stream()
|
||||
.map(it -> "|" + it.getName() + "|" + it.getTarget().getName())
|
||||
.collect(Collectors.joining());
|
||||
|
||||
assertThat(joinedNames).isEqualTo("|source a|target 1|source a2|target 1b");
|
||||
|
||||
final List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(1);
|
||||
assertSql(sql.get(0)).contains("select t0.dtype, t0.id, t0.name, t0.pos, t1.dtype, t1.id, t1.name from source_base t0 left join target_base t1 on t1.id = t0.target_id and t1.dtype = 'Target1' where t0.dtype = 'SourceA' order by t0.pos");
|
||||
}
|
||||
|
||||
/**
|
||||
* Expect separate lazy loading for SourceAs and SourceBs.
|
||||
*/
|
||||
private void assertFetchAllDoubleLazyLoading() {
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
final List<SourceBase> sources = DB.find(SourceBase.class).order("pos").findList();
|
||||
for (SourceBase source : sources) {
|
||||
if (source instanceof SourceA) {
|
||||
SourceA a = (SourceA) source;
|
||||
final Target1 target = a.getTarget();
|
||||
System.out.println("read target 1 " + target.getId() + " " + target.getName());
|
||||
|
||||
} else if (source instanceof SourceB) {
|
||||
SourceB b = (SourceB) source;
|
||||
final Target2 target = b.getTarget();
|
||||
System.out.println("read target 2 " + target.getId() + " " + target.getName());
|
||||
}
|
||||
}
|
||||
|
||||
final List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
assertThat(sql).hasSize(3);
|
||||
//assertSql(sql.get(0)).contains("select t0.dtype, t0.id, t0.name, t0.pos, t0.target_id, t0.target_id from source_base t0 order by t0.pos");
|
||||
assertSql(sql.get(0)).contains("select t0.dtype, t0.id, t0.name, t0.pos, t1.dtype, t0.target_id, t1.dtype, t0.target_id from source_base t0 left join target_base t1 on t1.id = t0.target_id order by t0.pos");
|
||||
assertSql(sql.get(1)).contains("select t0.dtype, t0.id, t0.name from target_base t0 where t0.dtype = 'Target1' and t0.id ");
|
||||
assertSql(sql.get(2)).contains("select t0.dtype, t0.id, t0.name from target_base t0 where t0.dtype = 'Target2' and t0.id = ?");
|
||||
}
|
||||
|
||||
private void assertSourceBaseEqual(SourceBase foundA, SourceBase sourceA) {
|
||||
assertThat(foundA.getId()).isEqualTo(sourceA.getId());
|
||||
assertThat(foundA.getName()).isEqualTo(sourceA.getName());
|
||||
}
|
||||
|
||||
private void assertSourceAEqual(SourceA found, SourceA source) {
|
||||
final Target1 target = found.getTarget();
|
||||
final String name = target.getName();
|
||||
assertThat(name).isEqualTo(source.getTarget().getName());
|
||||
|
||||
assertThat(found.getTarget().getId()).isEqualTo(source.getTarget().getId());
|
||||
assertThat(found.getTarget().getName()).isEqualTo(source.getTarget().getName());
|
||||
assertThat(found.getTarget().getClass()).isEqualTo(source.getTarget().getClass());
|
||||
}
|
||||
|
||||
private void assertSourceBEqual(SourceB found, SourceB source) {
|
||||
|
||||
final Target2 target = found.getTarget();
|
||||
final String name = target.getName();
|
||||
assertThat(name).isEqualTo(source.getTarget().getName());
|
||||
|
||||
assertThat(found.getTarget().getId()).isEqualTo(source.getTarget().getId());
|
||||
assertThat(found.getTarget().getName()).isEqualTo(source.getTarget().getName());
|
||||
assertThat(found.getTarget().getClass()).isEqualTo(source.getTarget().getClass());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.tests.inheritance.bothsides;
|
||||
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.MappedSuperclass;
|
||||
import java.util.UUID;
|
||||
|
||||
@MappedSuperclass
|
||||
public class WithAutoGeneratedUUID {
|
||||
|
||||
@Id
|
||||
UUID id;
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(UUID id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.tests.inheritance.cache;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
public class CIAddress extends CIBaseModel {
|
||||
|
||||
@ManyToOne
|
||||
public CIStreetParent street;
|
||||
|
||||
public CIStreetParent getStreet() {
|
||||
return street;
|
||||
}
|
||||
|
||||
public void setStreet(CIStreetParent street) {
|
||||
this.street = street;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.tests.inheritance.cache;
|
||||
|
||||
import io.ebean.Model;
|
||||
import io.ebean.annotation.Cache;
|
||||
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.MappedSuperclass;
|
||||
|
||||
@Cache(enableQueryCache=true)
|
||||
@MappedSuperclass
|
||||
public abstract class CIBaseModel extends Model {
|
||||
|
||||
@Id
|
||||
protected long id;
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.tests.inheritance.cache;
|
||||
|
||||
import javax.persistence.DiscriminatorValue;
|
||||
import javax.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
@DiscriminatorValue(value="1")
|
||||
public class CICustomer extends CICustomerParent {
|
||||
|
||||
public String notes;
|
||||
|
||||
public String getNotes() {
|
||||
return notes;
|
||||
}
|
||||
|
||||
public void setNotes(String notes) {
|
||||
this.notes = notes;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package org.tests.inheritance.cache;
|
||||
|
||||
import javax.persistence.DiscriminatorColumn;
|
||||
import javax.persistence.DiscriminatorType;
|
||||
import javax.persistence.DiscriminatorValue;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Inheritance;
|
||||
import javax.persistence.InheritanceType;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
|
||||
@DiscriminatorColumn(name = "dtype", discriminatorType = DiscriminatorType.INTEGER)
|
||||
@DiscriminatorValue(value = "0")
|
||||
public class CICustomerParent extends CIBaseModel {
|
||||
|
||||
@ManyToOne
|
||||
protected CIAddress address;
|
||||
|
||||
public CIAddress getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(CIAddress adress) {
|
||||
this.address = adress;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.tests.inheritance.cache;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.DiscriminatorValue;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "street")
|
||||
@DiscriminatorValue(value = "1")
|
||||
public class CIStreet extends CIStreetParent {
|
||||
|
||||
@Column(name="num")
|
||||
protected String number;
|
||||
|
||||
public String getNumber() {
|
||||
return number;
|
||||
}
|
||||
|
||||
public void setNumber(String number) {
|
||||
this.number = number;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package org.tests.inheritance.cache;
|
||||
|
||||
import javax.persistence.DiscriminatorColumn;
|
||||
import javax.persistence.DiscriminatorType;
|
||||
import javax.persistence.DiscriminatorValue;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Inheritance;
|
||||
import javax.persistence.InheritanceType;
|
||||
|
||||
@Entity
|
||||
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
|
||||
@DiscriminatorColumn(name = "dtype", discriminatorType = DiscriminatorType.INTEGER)
|
||||
@DiscriminatorValue(value = "0")
|
||||
public class CIStreetParent extends CIBaseModel {
|
||||
|
||||
protected String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
package org.tests.inheritance.cache;
|
||||
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class TestInheritCacheRefLoad {
|
||||
|
||||
@Test
|
||||
public void findWithRefToInheritStreetBean_expect_correctStreetType() {
|
||||
|
||||
DB.find(CICustomerParent.class).delete();
|
||||
DB.find(CIAddress.class).delete();
|
||||
DB.find(CIStreet.class).delete();
|
||||
|
||||
//=========================================================================
|
||||
// 1 - Create Data (Customer(dtype 1) -> Address(no dtype) -> Street(dtype 1)
|
||||
//=========================================================================
|
||||
CIStreet street = new CIStreet();
|
||||
street.save();
|
||||
CIAddress address = new CIAddress();
|
||||
address.setStreet(street);
|
||||
address.save();
|
||||
CICustomer customer = new CICustomer();
|
||||
customer.setAddress(address);
|
||||
customer.save();
|
||||
|
||||
//=========================================================================
|
||||
// 2 - Read Data (no Cache Hit)
|
||||
//=========================================================================
|
||||
Class streetClass = reloadCustomer(customer, false); //returns Street -> OK
|
||||
assertEquals("org.tests.inheritance.cache.CIStreet", streetClass.getName());
|
||||
streetClass = reloadCustomer(customer, false); //returns Street -> OK
|
||||
assertEquals("org.tests.inheritance.cache.CIStreet", streetClass.getName());
|
||||
|
||||
//=========================================================================
|
||||
// 3 - Read Data (L2-Cache Hit)
|
||||
//=========================================================================
|
||||
streetClass = reloadCustomer(customer, true); //returns Street -> OK
|
||||
assertEquals("org.tests.inheritance.cache.CIStreet", streetClass.getName());
|
||||
streetClass = reloadCustomer(customer, true); //returns StreetParent -> NOT OK
|
||||
assertEquals("org.tests.inheritance.cache.CIStreet", streetClass.getName());
|
||||
}
|
||||
|
||||
|
||||
public Class reloadCustomer(CICustomer customer, boolean l2Cache) {
|
||||
|
||||
// Load Customer via Query, L2Cache on/off
|
||||
CICustomer customerReloaded =
|
||||
DB.find(CICustomer.class)
|
||||
.where().eq("id", customer.getId())
|
||||
.setUseCache(l2Cache)
|
||||
.findOne();
|
||||
|
||||
//Access Street by lazy Loading
|
||||
Class streetClassLazyLoaded = customerReloaded.getAddress().getStreet().getClass();
|
||||
|
||||
//Show Cache Hits
|
||||
System.out.println("Class of Street (Cache on: " + l2Cache + "): " + streetClassLazyLoaded + " | Cache Hit: " + DB.getDefault().cacheManager().queryCache(streetClassLazyLoaded).statistics(false).getHitCount());
|
||||
return streetClassLazyLoaded;
|
||||
}
|
||||
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package org.tests.inheritance.cache;
|
||||
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.cache.CInhOne;
|
||||
import org.tests.model.basic.cache.CInhRoot;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestInheritanceCache extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
CInhOne one = new CInhOne();
|
||||
one.setLicenseNumber("O12");
|
||||
one.setDriver("Jimmy");
|
||||
one.setNotes("Hello");
|
||||
|
||||
DB.save(one);
|
||||
|
||||
CInhRoot gotOne = DB.find(CInhRoot.class)
|
||||
.setId(one.getId())
|
||||
.findOne();
|
||||
|
||||
assertThat(gotOne).isInstanceOf(CInhOne.class);
|
||||
|
||||
CInhRoot gotOneFromCache = DB.find(CInhRoot.class)
|
||||
.setId(one.getId())
|
||||
.findOne();
|
||||
|
||||
assertThat(gotOneFromCache).isInstanceOf(CInhOne.class);
|
||||
|
||||
CInhRoot refOne = DB.reference(CInhRoot.class, one.getId());
|
||||
assertThat(refOne).isInstanceOf(CInhOne.class);
|
||||
|
||||
CInhRoot refOneSub = DB.reference(CInhOne.class, one.getId());
|
||||
assertThat(refOneSub).isNotNull();
|
||||
}
|
||||
}
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
package org.tests.inheritance.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.cache.CInhOne;
|
||||
import org.tests.model.basic.cache.CInhRef;
|
||||
import org.tests.model.basic.cache.CInhRoot;
|
||||
import org.tests.model.basic.cache.CInhTwo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestInheritanceRefCache extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
CInhOne one = new CInhOne();
|
||||
one.setLicenseNumber("O12");
|
||||
one.setDriver("Jimmy");
|
||||
one.setNotes("Hello");
|
||||
|
||||
DB.save(one);
|
||||
|
||||
CInhRef ref = new CInhRef();
|
||||
ref.setRef(one);
|
||||
|
||||
DB.save(ref);
|
||||
|
||||
Integer id = ref.getId();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
CInhRef gotRef = DB.find(CInhRef.class).setId(id).findOne();
|
||||
|
||||
assertThat(gotRef).isInstanceOf(CInhRef.class);
|
||||
assertThat(gotRef.getRef()).isInstanceOf(CInhOne.class);
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(1);
|
||||
assertSql(sql.get(0)).contains("from cinh_ref").contains("left join cinh_root");
|
||||
|
||||
// fetch again - from cache (but fetch second bean from cache)
|
||||
LoggedSqlCollector.start();
|
||||
gotRef = DB.find(CInhRef.class).setId(id).findOne();
|
||||
|
||||
assertThat(gotRef).isInstanceOf(CInhRef.class);
|
||||
assertThat(gotRef.getRef()).isInstanceOf(CInhOne.class);
|
||||
sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(0);
|
||||
|
||||
|
||||
// fetch again - both from cache
|
||||
LoggedSqlCollector.start();
|
||||
gotRef = DB.find(CInhRef.class).setId(id).findOne();
|
||||
|
||||
assertThat(gotRef).isInstanceOf(CInhRef.class);
|
||||
assertThat(gotRef.getRef()).isInstanceOf(CInhOne.class);
|
||||
sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(0);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMap() {
|
||||
|
||||
List<Integer> ids = new ArrayList<>();
|
||||
|
||||
CInhOne one = new CInhOne();
|
||||
one.setLicenseNumber("O19");
|
||||
one.setDriver("Foo");
|
||||
one.setNotes("Hello");
|
||||
DB.save(one);
|
||||
|
||||
ids.add(one.getId());
|
||||
|
||||
CInhTwo two = new CInhTwo();
|
||||
two.setLicenseNumber("T23");
|
||||
two.setAction("Test");
|
||||
DB.save(two);
|
||||
|
||||
ids.add(two.getId());
|
||||
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
DB.find(CInhRoot.class).setUseQueryCache(true).where().idIn(ids).setMapKey("licenseNumber").findMap();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(1);
|
||||
assertSql(sql.get(0)).contains("from cinh_root");
|
||||
|
||||
// try some cache finds
|
||||
DB.find(CInhRoot.class).setUseQueryCache(true).findList();
|
||||
DB.find(CInhRoot.class).setUseQueryCache(true).findList();
|
||||
DB.find(CInhRoot.class).setUseQueryCache(true).findList();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
DB.find(CInhRoot.class).setUseQueryCache(true).where().idIn(ids).setMapKey("licenseNumber").findMap();
|
||||
|
||||
sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package org.tests.inheritance.cascadedelete;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.OneToMany;
|
||||
import java.util.List;
|
||||
|
||||
import static javax.persistence.CascadeType.ALL;
|
||||
|
||||
@Entity
|
||||
public class ComplexBean extends RootBean {
|
||||
|
||||
@OneToMany(cascade = ALL)
|
||||
private List<ElementBean> elements;
|
||||
|
||||
public ComplexBean(List<ElementBean> elements) {
|
||||
this.elements = elements;
|
||||
}
|
||||
|
||||
public List<ElementBean> getElements() {
|
||||
return elements;
|
||||
}
|
||||
|
||||
public void setElements(List<ElementBean> elements) {
|
||||
this.elements = elements;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package org.tests.inheritance.cascadedelete;
|
||||
|
||||
import io.ebean.annotation.NotNull;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class ElementBean {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@NotNull
|
||||
private String value;
|
||||
|
||||
public ElementBean(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package org.tests.inheritance.cascadedelete;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static javax.persistence.CascadeType.ALL;
|
||||
|
||||
@Entity
|
||||
public class ReferencingBean {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
public UUID id;
|
||||
|
||||
@OneToMany(cascade = ALL)
|
||||
private List<RootBean> rootBeans;
|
||||
|
||||
public ReferencingBean(List<RootBean> rootBeans) {
|
||||
this.rootBeans = rootBeans;
|
||||
}
|
||||
|
||||
public List<RootBean> getRootBeans() {
|
||||
return rootBeans;
|
||||
}
|
||||
|
||||
public void setRootBeans(List<RootBean> rootBeans) {
|
||||
this.rootBeans = rootBeans;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package org.tests.inheritance.cascadedelete;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Inheritance;
|
||||
import javax.persistence.PreRemove;
|
||||
import java.util.UUID;
|
||||
|
||||
@Entity
|
||||
@Inheritance
|
||||
public abstract class RootBean {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
public UUID id;
|
||||
|
||||
// added as workaround for the issue
|
||||
@PreRemove
|
||||
public void preRemove() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package org.tests.inheritance.cascadedelete;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
public class SimpleBean extends RootBean {
|
||||
|
||||
private String value;
|
||||
|
||||
public SimpleBean(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package org.tests.inheritance.cascadedelete;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
public class TestInheritanceDeleteCascade extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
RootBean bean1 = new ComplexBean(asList(new ElementBean("element-1"), new ElementBean("element-2")));
|
||||
RootBean bean2 = new SimpleBean("simple-1");
|
||||
ReferencingBean referencingBean = new ReferencingBean(asList(bean1, bean2));
|
||||
|
||||
DB.save(referencingBean);
|
||||
DB.delete(referencingBean);
|
||||
|
||||
assertNull(DB.find(RootBean.class, DB.beanId(bean1)));
|
||||
assertNull(DB.find(RootBean.class, DB.beanId(bean2)));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package org.tests.inheritance.company.domain;
|
||||
|
||||
import javax.persistence.DiscriminatorColumn;
|
||||
import javax.persistence.DiscriminatorType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Inheritance;
|
||||
import javax.persistence.InheritanceType;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Version;
|
||||
|
||||
/**
|
||||
* @author Per-Ingemar Andersson, It-huset i Norden AB
|
||||
*/
|
||||
|
||||
@Entity
|
||||
@Table(name = "bar")
|
||||
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
|
||||
@DiscriminatorColumn(name = "bar_type", discriminatorType = DiscriminatorType.STRING)
|
||||
//@MappedSuperclass
|
||||
public abstract class AbstractBar {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private int barId;
|
||||
|
||||
@ManyToOne(optional = false, fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "foo_id", nullable = false)
|
||||
private Foo foo;
|
||||
|
||||
@Version
|
||||
private int version;
|
||||
|
||||
public void setBarId(int barId) {
|
||||
this.barId = barId;
|
||||
}
|
||||
|
||||
public int getBarId() {
|
||||
return barId;
|
||||
}
|
||||
|
||||
public Foo getFoo() {
|
||||
return foo;
|
||||
}
|
||||
|
||||
public void setFoo(Foo foo) {
|
||||
this.foo = foo;
|
||||
}
|
||||
|
||||
public int getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(int version) {
|
||||
this.version = version;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package org.tests.inheritance.company.domain;
|
||||
|
||||
import javax.persistence.DiscriminatorValue;
|
||||
import javax.persistence.Entity;
|
||||
|
||||
/**
|
||||
* @author Per-Ingemar Andersson, It-huset i Norden AB
|
||||
*/
|
||||
@Entity
|
||||
@DiscriminatorValue("ConcreteBar")
|
||||
public class ConcreteBar extends AbstractBar {
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package org.tests.inheritance.company.domain;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Version;
|
||||
|
||||
/**
|
||||
* @author Per-Ingemar Andersson, It-huset i Norden AB
|
||||
*/
|
||||
@Entity
|
||||
@Table(name = "foo")
|
||||
public class Foo {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private int fooId;
|
||||
|
||||
private String importantText;
|
||||
|
||||
@Version
|
||||
private int version;
|
||||
|
||||
public void setFooId(int fooId) {
|
||||
this.fooId = fooId;
|
||||
}
|
||||
|
||||
public int getFooId() {
|
||||
return fooId;
|
||||
}
|
||||
|
||||
public String getImportantText() {
|
||||
return importantText;
|
||||
}
|
||||
|
||||
public void setImportantText(String importantText) {
|
||||
this.importantText = importantText;
|
||||
}
|
||||
|
||||
public int getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(int version) {
|
||||
this.version = version;
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package org.tests.inheritance.company.domain;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestInheritAbstract {
|
||||
|
||||
@Test
|
||||
public void testMe() {
|
||||
|
||||
Database server = DB.getDefault();
|
||||
|
||||
List<AbstractBar> list0 = server.find(AbstractBar.class)
|
||||
.findList();
|
||||
|
||||
assertNotNull(list0);
|
||||
|
||||
List<AbstractBar> list1 = server.find(AbstractBar.class)
|
||||
.fetch("foo", "importantText")
|
||||
.findList();
|
||||
|
||||
assertNotNull(list1);
|
||||
|
||||
Foo f = new Foo();
|
||||
f.setImportantText("blah");
|
||||
|
||||
server.save(f);
|
||||
|
||||
ConcreteBar cb = new ConcreteBar();
|
||||
cb.setFoo(f);
|
||||
server.save(cb);
|
||||
|
||||
List<AbstractBar> list2 = server.find(AbstractBar.class)
|
||||
.fetch("foo", "importantText")
|
||||
.findList();
|
||||
|
||||
assertNotNull(list2);
|
||||
assertThat(list2).isNotEmpty();
|
||||
|
||||
for (AbstractBar abstractBar : list2) {
|
||||
Foo foo = abstractBar.getFoo();
|
||||
String importantText = foo.getImportantText();
|
||||
assertNotNull(importantText);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.tests.inheritance.model;
|
||||
|
||||
import javax.persistence.MappedSuperclass;
|
||||
|
||||
@MappedSuperclass
|
||||
public class AbstractBaseClass {
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package org.tests.inheritance.model;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
public class CalculationResult {
|
||||
|
||||
@Id
|
||||
@Column(name = "id")
|
||||
private Integer id;
|
||||
|
||||
private double charge;
|
||||
|
||||
@ManyToOne(cascade = CascadeType.PERSIST)
|
||||
private ProductConfiguration productConfiguration;
|
||||
|
||||
@ManyToOne(cascade = CascadeType.PERSIST)
|
||||
private GroupConfiguration groupConfiguration;
|
||||
|
||||
public double getCharge() {
|
||||
return charge;
|
||||
}
|
||||
|
||||
public void setCharge(double charge) {
|
||||
this.charge = charge;
|
||||
}
|
||||
|
||||
public ProductConfiguration getProductConfiguration() {
|
||||
return productConfiguration;
|
||||
}
|
||||
|
||||
public void setProductConfiguration(ProductConfiguration productConfiguration) {
|
||||
this.productConfiguration = productConfiguration;
|
||||
}
|
||||
|
||||
public GroupConfiguration getGroupConfiguration() {
|
||||
return groupConfiguration;
|
||||
}
|
||||
|
||||
public void setGroupConfiguration(GroupConfiguration groupConfiguration) {
|
||||
this.groupConfiguration = groupConfiguration;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package org.tests.inheritance.model;
|
||||
|
||||
import io.ebean.annotation.Cache;
|
||||
import io.ebean.annotation.ChangeLog;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@ChangeLog
|
||||
@Entity
|
||||
@Cache(enableQueryCache = true)
|
||||
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
|
||||
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING, columnDefinition = "varchar(21)")
|
||||
public class Configuration extends AbstractBaseClass {
|
||||
|
||||
@Id
|
||||
@Column(name = "id")
|
||||
private Integer id;
|
||||
|
||||
|
||||
@ManyToOne
|
||||
private Configurations configurations;
|
||||
|
||||
|
||||
public Configuration() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Configurations getConfigurations() {
|
||||
return configurations;
|
||||
}
|
||||
|
||||
public void setConfigurations(Configurations configurations) {
|
||||
this.configurations = configurations;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package org.tests.inheritance.model;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class Configurations {
|
||||
@Id
|
||||
@Column(name = "id")
|
||||
private Integer id;
|
||||
|
||||
private String name;
|
||||
|
||||
@OneToMany
|
||||
private List<GroupConfiguration> groupConfigurations;
|
||||
|
||||
@OneToMany
|
||||
private List<ProductConfiguration> productConfigurations;
|
||||
|
||||
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 List<GroupConfiguration> getGroupConfigurations() {
|
||||
return groupConfigurations;
|
||||
}
|
||||
|
||||
public void setGroupConfigurations(List<GroupConfiguration> groupConfigurations) {
|
||||
this.groupConfigurations = groupConfigurations;
|
||||
}
|
||||
|
||||
public void addGroupConfiguration(GroupConfiguration groupConfiguration) {
|
||||
groupConfiguration.setConfigurations(this);
|
||||
groupConfigurations.add(groupConfiguration);
|
||||
}
|
||||
|
||||
public List<ProductConfiguration> getProductConfigurations() {
|
||||
return productConfigurations;
|
||||
}
|
||||
|
||||
public void setProductConfigurations(List<ProductConfiguration> productConfigurations) {
|
||||
this.productConfigurations = productConfigurations;
|
||||
}
|
||||
|
||||
public void addProductConfiguration(ProductConfiguration productConfiguration) {
|
||||
productConfiguration.setConfigurations(this);
|
||||
productConfigurations.add(productConfiguration);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package org.tests.inheritance.model;
|
||||
|
||||
import javax.persistence.DiscriminatorValue;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.OneToMany;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@DiscriminatorValue("2")
|
||||
public class GroupConfiguration extends Configuration {
|
||||
private String groupName;
|
||||
|
||||
@OneToMany(mappedBy = "groupConfiguration")
|
||||
private List<CalculationResult> results;
|
||||
|
||||
public GroupConfiguration() {
|
||||
super();
|
||||
}
|
||||
|
||||
public GroupConfiguration(String name) {
|
||||
super();
|
||||
this.groupName = name;
|
||||
}
|
||||
|
||||
public String getGroupName() {
|
||||
return groupName;
|
||||
}
|
||||
|
||||
public void setGroupName(String groupName) {
|
||||
this.groupName = groupName;
|
||||
}
|
||||
|
||||
public List<CalculationResult> getResults() {
|
||||
return results;
|
||||
}
|
||||
|
||||
public void setResults(List<CalculationResult> results) {
|
||||
this.results = results;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package org.tests.inheritance.model;
|
||||
|
||||
import javax.persistence.DiscriminatorValue;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.OneToMany;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@DiscriminatorValue("1")
|
||||
public class ProductConfiguration extends Configuration {
|
||||
private String productName;
|
||||
|
||||
@OneToMany(mappedBy = "productConfiguration")
|
||||
private List<CalculationResult> results;
|
||||
|
||||
public ProductConfiguration() {
|
||||
super();
|
||||
}
|
||||
|
||||
public String getProductName() {
|
||||
return productName;
|
||||
}
|
||||
|
||||
public void setProductName(String productName) {
|
||||
this.productName = productName;
|
||||
}
|
||||
|
||||
public List<CalculationResult> getResults() {
|
||||
return results;
|
||||
}
|
||||
|
||||
public void setResults(List<CalculationResult> results) {
|
||||
this.results = results;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package org.tests.inheritance.order;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.OrderColumn;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class OrderMasterInheritance {
|
||||
|
||||
@Id
|
||||
Integer id;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL)
|
||||
@OrderColumn(name = "sort_order")
|
||||
List<OrderedParent> referenced = new ArrayList<>();
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public List<OrderedParent> getReferenced() {
|
||||
return referenced;
|
||||
}
|
||||
|
||||
public void setReferenced(final List<OrderedParent> referenced) {
|
||||
this.referenced = referenced;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package org.tests.inheritance.order;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
public class OrderedA extends OrderedParent {
|
||||
|
||||
String orderedAName;
|
||||
|
||||
public String getOrderedAName() {
|
||||
return orderedAName;
|
||||
}
|
||||
|
||||
public void setOrderedAName(final String orderedAName) {
|
||||
this.orderedAName = orderedAName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package org.tests.inheritance.order;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
public class OrderedB extends OrderedParent {
|
||||
|
||||
String orderedBName;
|
||||
|
||||
public String getOrderedBName() {
|
||||
return orderedBName;
|
||||
}
|
||||
|
||||
public void setOrderedBName(final String orderedBName) {
|
||||
this.orderedBName = orderedBName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package org.tests.inheritance.order;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Inheritance;
|
||||
|
||||
@Entity
|
||||
@Inheritance
|
||||
public abstract class OrderedParent {
|
||||
|
||||
@Id
|
||||
Integer id;
|
||||
|
||||
String commonName;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getCommonName() {
|
||||
return commonName;
|
||||
}
|
||||
|
||||
public void setCommonName(final String commonName) {
|
||||
this.commonName = commonName;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user