From fc0b47342bbd7d06d3a3927bb0864f760fc886b4 Mon Sep 17 00:00:00 2001 From: Robin Bygrave Date: Thu, 29 Oct 2015 22:01:28 +1300 Subject: [PATCH] #436 - @DiscriminatorColumn mapped also as @Column leads to NullPointerException --- .../deploy/meta/DeployBeanPropertyLists.java | 43 +++++++++++++------ .../inheritance/TestInheritanceOnMany.java | 15 ++++--- .../com/avaje/tests/model/basic/Animal.java | 12 ++++++ 3 files changed, 49 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/avaje/ebeaninternal/server/deploy/meta/DeployBeanPropertyLists.java b/src/main/java/com/avaje/ebeaninternal/server/deploy/meta/DeployBeanPropertyLists.java index 155c1acbc..0e0c6af68 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/deploy/meta/DeployBeanPropertyLists.java +++ b/src/main/java/com/avaje/ebeaninternal/server/deploy/meta/DeployBeanPropertyLists.java @@ -73,7 +73,31 @@ public class DeployBeanPropertyLists { this.propertyMap = new LinkedHashMap(); + // see if there is a discriminator property we should add + String discriminatorColumn = null; + BeanProperty discProperty = null; + + InheritInfo inheritInfo = deploy.getInheritInfo(); + if (inheritInfo != null) { + // Create a BeanProperty for the discriminator column to support + // using RawSql queries with inheritance + discriminatorColumn = inheritInfo.getDiscriminatorColumn(); + DeployBeanProperty discDeployProp = new DeployBeanProperty(deploy, String.class, new ScalarTypeString(), null); + discDeployProp.setDiscriminator(); + discDeployProp.setName(discriminatorColumn); + discDeployProp.setDbColumn(discriminatorColumn); + + // only register it in the propertyMap. This might not be used if + // an explicit property is mapped to the discriminator on the bean + discProperty = new BeanProperty(desc, discDeployProp); + } + for (DeployBeanProperty prop : deploy.propertiesAll()) { + if (discriminatorColumn != null && discriminatorColumn.equals(prop.getDbColumn())) { + // we have an explicit property mapped to the discriminator column + prop.setDiscriminator(); + discProperty = null; + } BeanProperty beanProp = createBeanProperty(owner, prop); propertyMap.put(beanProp.getName(), beanProp); } @@ -84,21 +108,12 @@ public class DeployBeanPropertyLists { allocateToList(prop); } - InheritInfo inheritInfo = deploy.getInheritInfo(); - if (inheritInfo != null) { - // Create a BeanProperty for the discriminator column to support - // using RawSql queries with inheritance - String discriminatorColumn = inheritInfo.getDiscriminatorColumn(); - DeployBeanProperty discDeployProp = new DeployBeanProperty(deploy, String.class, new ScalarTypeString(), null); - discDeployProp.setDiscriminator(); - discDeployProp.setName(discriminatorColumn); - discDeployProp.setDbColumn(discriminatorColumn); - - // create the discriminator BeanProperty and only register it in the propertyMap - BeanProperty dprop = new BeanProperty(desc, discDeployProp); - propertyMap.put(dprop.getName(), dprop); + if (discProperty != null) { + // put the discriminator property into the property map only + // (after the real properties have been organised into their lists) + propertyMap.put(discProperty.getName(), discProperty); } - + List deployTableJoins = deploy.getTableJoins(); tableJoins = new TableJoin[deployTableJoins.size()]; for (int i = 0; i < deployTableJoins.size(); i++) { diff --git a/src/test/java/com/avaje/tests/inheritance/TestInheritanceOnMany.java b/src/test/java/com/avaje/tests/inheritance/TestInheritanceOnMany.java index b0500ec55..4aa3bb795 100644 --- a/src/test/java/com/avaje/tests/inheritance/TestInheritanceOnMany.java +++ b/src/test/java/com/avaje/tests/inheritance/TestInheritanceOnMany.java @@ -1,10 +1,5 @@ package com.avaje.tests.inheritance; -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.bean.BeanCollection.ModifyListenMode; @@ -13,6 +8,11 @@ import com.avaje.tests.model.basic.Animal; import com.avaje.tests.model.basic.AnimalShelter; import com.avaje.tests.model.basic.Cat; import com.avaje.tests.model.basic.Dog; +import org.junit.Test; + +import java.util.List; + +import static org.junit.Assert.assertNotNull; public class TestInheritanceOnMany extends BaseTestCase { @@ -40,8 +40,9 @@ public class TestInheritanceOnMany extends BaseTestCase { BeanList beanList = (BeanList)animals; ModifyListenMode modifyListenMode = beanList.getModifyListenMode(); - Assert.assertNotNull(modifyListenMode); - + assertNotNull(modifyListenMode); + + assertNotNull(Ebean.find(Animal.class).findList()); } } diff --git a/src/test/java/com/avaje/tests/model/basic/Animal.java b/src/test/java/com/avaje/tests/model/basic/Animal.java index 10bec8617..bf9c7bd24 100644 --- a/src/test/java/com/avaje/tests/model/basic/Animal.java +++ b/src/test/java/com/avaje/tests/model/basic/Animal.java @@ -1,5 +1,6 @@ package com.avaje.tests.model.basic; +import javax.persistence.Column; import javax.persistence.DiscriminatorColumn; import javax.persistence.Entity; import javax.persistence.Id; @@ -21,6 +22,9 @@ public abstract class Animal { @Version Long version; + @Column(name="species", insertable = false, updatable = false, nullable = false) + String species; + @ManyToOne AnimalShelter shelter; @@ -40,6 +44,14 @@ public abstract class Animal { this.version = version; } + public String getSpecies() { + return species; + } + + public void setSpecies(String species) { + this.species = species; + } + public AnimalShelter getShelter() { return shelter; }