#919 - io.ebean package initial

This commit is contained in:
Rob Bygrave
2016-12-11 23:23:22 +13:00
parent 5821dc96b9
commit 971e2dc91b
2134 changed files with 10721 additions and 8652 deletions
@@ -0,0 +1,32 @@
package org.tests.model.selfref;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import java.util.UUID;
@MappedSuperclass
public abstract class BaseResourceFile {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "id", nullable = false, length = 64)
private String id;
public BaseResourceFile() {
this.id = newId();
}
public String getId() {
return id;
}
protected void setId(String id) {
this.id = id;
}
protected String newId() {
return UUID.randomUUID().toString();
}
}
@@ -0,0 +1,56 @@
package org.tests.model.selfref;
import io.ebean.annotation.PrivateOwned;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "resourcefile")
public class ResourceFile extends BaseResourceFile {
private static final long serialVersionUID = 1L;
@ManyToOne(fetch = FetchType.LAZY, optional = true)
@JoinColumn(name = "parentResourceFileId", nullable = true)
private ResourceFile parent;
@OneToMany(cascade = CascadeType.REMOVE, fetch = FetchType.LAZY, mappedBy = "parent")
@PrivateOwned
private Set<ResourceFile> alternatives = new HashSet<>();
@Column(name = "name", length = 128, nullable = false)
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ResourceFile getParent() {
return parent;
}
public void setParent(ResourceFile parent) {
this.parent = parent;
}
public Set<ResourceFile> getAlternatives() {
return alternatives;
}
public void setAlternatives(Set<ResourceFile> alternatives) {
this.alternatives = alternatives;
}
}
@@ -0,0 +1,77 @@
package org.tests.model.selfref;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Version;
import java.util.List;
@Entity
@Table(name = "self_parent")
public class SelfParent {
@Id
Long id;
@Version
Long version;
String name;
@ManyToOne()
SelfParent parent;
@OneToMany(mappedBy = "parent")
List<SelfParent> children;
public SelfParent(String name, SelfParent parent) {
this.name = name;
this.parent = parent;
}
public SelfParent() {
}
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 SelfParent getParent() {
return parent;
}
public void setParent(SelfParent parent) {
this.parent = parent;
}
public List<SelfParent> getChildren() {
return children;
}
public void setChildren(List<SelfParent> children) {
this.children = children;
}
}
@@ -0,0 +1,46 @@
package org.tests.model.selfref;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "self_ref_customer")
public class SelfRefCustomer {
@Id
Long id;
String name;
@ManyToOne
@JoinColumn(name = "referred_by_id")
SelfRefCustomer referredBy;
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 SelfRefCustomer getReferredBy() {
return referredBy;
}
public void setReferredBy(SelfRefCustomer referredBy) {
this.referredBy = referredBy;
}
}
@@ -0,0 +1,87 @@
package org.tests.model.selfref;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import java.util.List;
@Entity
public class SelfRefExample {
@Id
private Long id;
@Column(nullable = false)
private String name;
@ManyToOne
private SelfRefExample parent;
@OrderBy("id")
@OneToMany(mappedBy = "parent")
private List<SelfRefExample> children;
public SelfRefExample(String name, SelfRefExample parent) {
this.name = name;
this.parent = parent;
}
/**
* @return the id
*/
public Long getId() {
return this.id;
}
/**
* @param id the id to set
*/
public void setId(Long id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return this.name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the parent
*/
public SelfRefExample getParent() {
return this.parent;
}
/**
* @param parent the parent to set
*/
public void setParent(SelfRefExample parent) {
this.parent = parent;
}
/**
* @return the children
*/
public List<SelfRefExample> getChildren() {
return this.children;
}
/**
* @param children the children to set
*/
public void setChildren(List<SelfRefExample> children) {
this.children = children;
}
}
@@ -0,0 +1,37 @@
package org.tests.model.selfref;
import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import io.ebean.EbeanServer;
import org.junit.Test;
public class TestResourceFileSelfRef extends BaseTestCase {
@Test
public void test() {
EbeanServer server = Ebean.getServer(null);
ResourceFile childFile1 = new ResourceFile();
childFile1.setName("childFile1");
ResourceFile childFile2 = new ResourceFile();
childFile2.setName("childFile2");
ResourceFile parentFile1 = new ResourceFile();
parentFile1.setName("parentFile1");
childFile1.setParent(parentFile1);
childFile2.setParent(parentFile1);
parentFile1.getAlternatives().add(childFile1);
parentFile1.getAlternatives().add(childFile2);
server.save(parentFile1);
server.save(childFile1);
server.save(childFile2);
// As a workaround for the problem, the child objects can be deleted first
//server.delete(childFile1);
//server.delete(childFile2);
server.delete(parentFile1);
}
}
@@ -0,0 +1,86 @@
package org.tests.model.selfref;
import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import io.ebean.Query;
import org.junit.Test;
import javax.persistence.PersistenceException;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
public class TestSelfRefExample extends BaseTestCase {
@Test
public void test() {
try {
Ebean.createSqlUpdate("delete from self_ref_example").execute();
} catch (PersistenceException e) {
logger.debug("TestSelfRefExample skipped - MySql not deleting based on constraints");
return;
}
SelfRefExample e1 = new SelfRefExample("test1", null);
SelfRefExample e2 = new SelfRefExample("test1", e1);
SelfRefExample e3 = new SelfRefExample("test2", e2);
SelfRefExample e4 = new SelfRefExample("test2", e1);
SelfRefExample e5 = new SelfRefExample("test2", e4);
SelfRefExample e6 = new SelfRefExample("test2", e2);
SelfRefExample e7 = new SelfRefExample("test3", e3);
SelfRefExample e8 = new SelfRefExample("test3", e7);
Ebean.save(e1);
Ebean.save(e2);
Ebean.save(e3);
Ebean.save(e4);
Ebean.save(e5);
Ebean.save(e6);
Ebean.save(e7);
Ebean.save(e8);
Query<SelfRefExample> examples = Ebean.find(SelfRefExample.class).setLazyLoadBatchSize(1);
List<SelfRefExample> list = examples.where().eq("name", "test1").order().asc("id").findList();
assertThat(list).hasSize(2);
//The first Example is e1. e2 is the first child of e1. e3 is the first child of e2.
SelfRefExample e1Fetched = list.get(0);
SelfRefExample e2Fetched = list.get(1);
assertThat(e1Fetched.getId()).isEqualTo(e1.getId());
assertThat(e2Fetched.getId()).isEqualTo(e2.getId());
// Now we can't guarantee the order of loaded children so use findChild
List<SelfRefExample> e1Children = e1Fetched.getChildren();
SelfRefExample e2Searched = findChild(e1Children, e2.getId());
assertThat(e2Fetched.getChildren()).hasSize(2);
assertThat(e2Fetched.getChildren()).extracting("id").contains(e3.getId(), e6.getId());
SelfRefExample e3Searched = findChild(e2Searched.getChildren(), e3.getId());
assertThat(e3Searched).isNotNull();
assertThat(e3Searched.getChildren()).extracting("id").contains(e7.getId());
// If we get all the items, you can see the structure goes down a fair bit further.
Query<SelfRefExample> examples2 = Ebean.createQuery(SelfRefExample.class).orderBy("id asc");
List<SelfRefExample> list2 = examples2.findList();
assertEquals(e1.getId(), list2.get(0).getId());
assertEquals(e2.getId(), list2.get(0).getChildren().get(0).getId());
assertEquals(e3.getId(), list2.get(0).getChildren().get(0).getChildren().get(0).getId());
assertEquals(e7.getId(), list2.get(0).getChildren().get(0).getChildren().get(0).getChildren().get(0).getId());
}
private SelfRefExample findChild(List<SelfRefExample> e1Children, Long id) {
for (SelfRefExample e1Child : e1Children) {
if (e1Child.getId().equals(id)) {
return e1Child;
}
}
return null;
}
}
@@ -0,0 +1,55 @@
package org.tests.model.selfref;
import io.ebean.BaseTestCase;
import io.ebean.BeanState;
import io.ebean.Ebean;
import org.junit.Assert;
import org.junit.Test;
import java.util.List;
public class TestTextJsonSelfRef extends BaseTestCase {
@Test
public void test() {
Ebean.execute(() -> {
if (Ebean.find(SelfRefCustomer.class).findCount() == 0) {
SelfRefCustomer c1 = new SelfRefCustomer();
c1.setName("Foo");
c1.setReferredBy(c1);
SelfRefCustomer c2 = new SelfRefCustomer();
c2.setName("Bar");
c2.setReferredBy(c1);
SelfRefCustomer c3 = new SelfRefCustomer();
c3.setName("baz");
c3.setReferredBy(c1);
Ebean.save(c1);
Ebean.save(c2);
Ebean.save(c3);
}
});
List<SelfRefCustomer> customers = Ebean.find(SelfRefCustomer.class).orderBy("id desc").findList();
// Check that there are no 'reference' beans here
for (SelfRefCustomer cust : customers) {
BeanState beanState = Ebean.getBeanState(cust);
Assert.assertFalse(beanState.isReference());
}
// JsonWriteOptions options = JsonWriteOptions.parsePath("(id,name,referredBy(id))");
// String customerContent = Ebean.createJsonContext().toJson(customers);//, false, options);
// System.out.println("Customers: " + customerContent);
//
// Assert
// .assertEquals(
// "[{\"id\":3,\"name\":\"baz\",\"referredBy\":{\"id\":1}},{\"id\":2,\"name\":\"Bar\",\"referredBy\":{\"id\":1}},{\"id\":1,\"name\":\"Foo\",\"referredBy\":{\"id\":1}}]",
// customerContent);
}
}