mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-23 07:11:55 +00:00
Fix/create reference with descriptor using inheritance (#13)
* FIXED: Discriminator must be ask for inherited classes * Fixed: Compile * Refactoring and Testing
This commit is contained in:
1 parent
c57af29fca
commit
bccb9f8ddb
6 files changed
+183
-4
No files matched your search
@@ -1,10 +1,15 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebean.plugin.Property;
|
||||
import org.junit.Test;
|
||||
import org.tests.model.basic.Animal;
|
||||
import org.tests.model.basic.AnimalShelter;
|
||||
import org.tests.model.basic.Cat;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Dog;
|
||||
import org.tests.model.basic.Order;
|
||||
|
||||
import java.util.Collection;
|
||||
@@ -46,6 +51,29 @@ public class BeanDescriptorTest extends BaseTestCase {
|
||||
Customer bean = customerDesc.createReference(Boolean.FALSE, true, 42, null);
|
||||
assertThat(server().getBeanState(bean).isDisableLazyLoad()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createReference_with_inheritance() {
|
||||
Cat cat = new Cat();
|
||||
cat.setName("Puss");
|
||||
Ebean.save(cat);
|
||||
|
||||
Dog dog = new Dog();
|
||||
dog.setRegistrationNumber("DOGGIE");
|
||||
Ebean.save(dog);
|
||||
|
||||
AnimalShelter shelter = new AnimalShelter();
|
||||
shelter.setName("My Animal Shelter");
|
||||
shelter.getAnimals().add(cat);
|
||||
shelter.getAnimals().add(dog);
|
||||
|
||||
Ebean.save(shelter);
|
||||
|
||||
BeanDescriptor<Animal> animalDesc = spiEbeanServer().getBeanDescriptor(Animal.class);
|
||||
|
||||
Animal bean = animalDesc.createReference(Boolean.FALSE, false, dog.getId(), null);
|
||||
assertThat(bean.getId()).isEqualTo(dog.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allProperties() {
|
||||
|
||||
@@ -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,45 @@
|
||||
package org.tests.inheritance;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import io.ebean.annotation.PrivateOwned;
|
||||
|
||||
/**
|
||||
* Model class to reference an organization tree node.
|
||||
*
|
||||
* @author Christian Hartl, FOCONIS AG
|
||||
*/
|
||||
@Entity
|
||||
public class OrganizationTreeNode {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "parentTreeNode")
|
||||
@NotNull
|
||||
@PrivateOwned
|
||||
private OrganizationNode organizationNode;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
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,25 @@
|
||||
package org.tests.inheritance;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestTreeOrganisations extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
OrganizationTreeNode treeNode = new OrganizationTreeNode();
|
||||
OrganizationNode node = new OrganizationUnit();
|
||||
|
||||
treeNode.setOrganizationNode(node);
|
||||
Ebean.save(treeNode);
|
||||
|
||||
treeNode = Ebean.find(OrganizationTreeNode.class, treeNode.getId());
|
||||
assertEquals(node, treeNode.getOrganizationNode());
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user