Fix for #77 - NPE when lazy loading on a OneToMany that is not a leaf

This commit is contained in:
Rob Bygrave
2014-03-31 22:06:10 +13:00
parent 189e0a2ea9
commit 34347fc3e1
6 changed files with 217 additions and 1 deletions
@@ -0,0 +1,42 @@
package com.avaje.tests.model.inheritmany;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity
public class IMRelated {
@Id
Long id;
String name;
@ManyToOne(optional=false)
IMRoot owner;
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 IMRoot getOwner() {
return owner;
}
public void setOwner(IMRoot owner) {
this.owner = owner;
}
}
@@ -0,0 +1,37 @@
package com.avaje.tests.model.inheritmany;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.OneToMany;
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class IMRoot {
@Id
Long id;
@OneToMany(mappedBy="owner")
List<IMRelated> related;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public List<IMRelated> getRelated() {
return related;
}
public void setRelated(List<IMRelated> related) {
this.related = related;
}
}
@@ -0,0 +1,20 @@
package com.avaje.tests.model.inheritmany;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
@Entity
@DiscriminatorValue("ONE")
public class IMRootOne extends IMRoot {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@@ -0,0 +1,32 @@
package com.avaje.tests.model.inheritmany;
import java.util.Date;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
@Entity
@DiscriminatorValue("TWO")
public class IMRootTwo extends IMRoot {
String title;
Date whenTitle;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Date getWhenTitle() {
return whenTitle;
}
public void setWhenTitle(Date whenTitle) {
this.whenTitle = whenTitle;
}
}
@@ -0,0 +1,73 @@
package org.avaje.test.model.inheritmany.test;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import com.avaje.ebean.BaseTestCase;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.EbeanServer;
import com.avaje.ebeaninternal.api.SpiEbeanServer;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.InheritInfo;
import com.avaje.tests.model.inheritmany.IMRelated;
import com.avaje.tests.model.inheritmany.IMRoot;
import com.avaje.tests.model.inheritmany.IMRootOne;
import com.avaje.tests.model.inheritmany.IMRootTwo;
public class TestInheritWithMany extends BaseTestCase {
@Test
public void test() {
EbeanServer server = Ebean.getServer(null);
Assert.assertNotNull(server);
SpiEbeanServer spiServer = (SpiEbeanServer)server;
BeanDescriptor<IMRoot> beanDescriptor = spiServer.getBeanDescriptor(IMRoot.class);
InheritInfo inheritInfo = beanDescriptor.getInheritInfo();
Assert.assertNotNull(inheritInfo);
IMRootOne one = new IMRootOne();
one.setName("One Name");
server.save(one);
add(one, "aaa");
add(one, "bbb");
IMRootTwo two = new IMRootTwo();
two.setTitle("Two Title");
server.save(two);
add(two, "ccc");
add(two, "ddd");
List<IMRoot> list = server.find(IMRoot.class).select("id").findList();
for (IMRoot imRoot : list) {
// lazy load the related OneToMany which is related to a non-leaf
List<IMRelated> related = imRoot.getRelated();
for (IMRelated imRelated : related) {
imRelated.getName();
}
}
}
private void add(IMRoot owner, String string) {
IMRelated relate = new IMRelated();
relate.setName(string);
relate.setOwner(owner);
EbeanServer server = Ebean.getServer(null);
server.save(relate);
}
}