From 259814f4d9e332598b7abc4dd7f63b59579f2836 Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Wed, 16 Nov 2016 21:57:38 +1300 Subject: [PATCH] #849 - AbstractNamingConvention.getTableName inheritance logic differs from InheritInfo --- .../config/AbstractNamingConvention.java | 22 +++++++++++++++---- .../server/deploy/parse/AnnotationClass.java | 10 ++++++--- .../inheritance/TestInheritanceOnMany.java | 22 ++++++++++++------- .../com/avaje/tests/model/basic/Animal.java | 2 -- .../com/avaje/tests/model/basic/BigDog.java | 19 ++++++++++++++++ 5 files changed, 58 insertions(+), 17 deletions(-) create mode 100644 src/test/java/com/avaje/tests/model/basic/BigDog.java diff --git a/src/main/java/com/avaje/ebean/config/AbstractNamingConvention.java b/src/main/java/com/avaje/ebean/config/AbstractNamingConvention.java index 5a07a44ef..3f836a709 100644 --- a/src/main/java/com/avaje/ebean/config/AbstractNamingConvention.java +++ b/src/main/java/com/avaje/ebean/config/AbstractNamingConvention.java @@ -2,8 +2,10 @@ package com.avaje.ebean.config; import com.avaje.ebean.config.dbplatform.DatabasePlatform; +import javax.persistence.DiscriminatorValue; import javax.persistence.Inheritance; import javax.persistence.Table; +import java.lang.annotation.Annotation; /** * Provides some base implementation for NamingConventions. @@ -182,11 +184,9 @@ public abstract class AbstractNamingConvention implements NamingConvention { TableName tableName = getTableNameFromAnnotation(beanClass); if (tableName == null) { - Class supCls = beanClass.getSuperclass(); - Inheritance inheritance = supCls.getAnnotation(Inheritance.class); - if (inheritance != null) { - // get the table as per inherited class in case their + if (hasInheritance(supCls)) { + // get the table as per inherited class in case there // is not a table annotation in the inheritance hierarchy return getTableName(supCls); } @@ -207,6 +207,20 @@ public abstract class AbstractNamingConvention implements NamingConvention { return new TableName(catalog, schema, tableName.getName()); } + /** + * Return true if this class is part of entity inheritance. + */ + protected boolean hasInheritance(Class supCls) { + return hasAnnotation(supCls, Inheritance.class) || hasAnnotation(supCls, DiscriminatorValue.class); + } + + /** + * Return true if the class has the given annotation. + */ + protected boolean hasAnnotation(Class supCls, Class annotation) { + return supCls.getAnnotation(annotation) != null; + } + public TableName getM2MJoinTableName(TableName lhsTable, TableName rhsTable) { StringBuilder buffer = new StringBuilder(); diff --git a/src/main/java/com/avaje/ebeaninternal/server/deploy/parse/AnnotationClass.java b/src/main/java/com/avaje/ebeaninternal/server/deploy/parse/AnnotationClass.java index 3fe8be9a7..9aac99081 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/deploy/parse/AnnotationClass.java +++ b/src/main/java/com/avaje/ebeaninternal/server/deploy/parse/AnnotationClass.java @@ -14,6 +14,7 @@ import com.avaje.ebean.annotation.View; import com.avaje.ebean.config.TableName; import com.avaje.ebeaninternal.server.deploy.BeanDescriptor.EntityType; import com.avaje.ebeaninternal.server.deploy.IndexDefinition; +import com.avaje.ebeaninternal.server.deploy.InheritInfo; import com.avaje.ebeaninternal.server.deploy.meta.DeployBeanProperty; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -94,10 +95,13 @@ public class AnnotationClass extends AnnotationParser { private void setTableName() { if (descriptor.isBaseTableType()) { - + Class beanType = descriptor.getBeanType(); + InheritInfo inheritInfo = descriptor.getInheritInfo(); + if (inheritInfo != null) { + beanType = inheritInfo.getRoot().getType(); + } // default the TableName using NamingConvention. - TableName tableName = namingConvention.getTableName(descriptor.getBeanType()); - + TableName tableName = namingConvention.getTableName(beanType); descriptor.setBaseTable(tableName, asOfViewSuffix, versionsBetweenSuffix); } } diff --git a/src/test/java/com/avaje/tests/inheritance/TestInheritanceOnMany.java b/src/test/java/com/avaje/tests/inheritance/TestInheritanceOnMany.java index 4aa3bb795..90d7abc60 100644 --- a/src/test/java/com/avaje/tests/inheritance/TestInheritanceOnMany.java +++ b/src/test/java/com/avaje/tests/inheritance/TestInheritanceOnMany.java @@ -6,6 +6,7 @@ import com.avaje.ebean.bean.BeanCollection.ModifyListenMode; import com.avaje.ebean.common.BeanList; import com.avaje.tests.model.basic.Animal; import com.avaje.tests.model.basic.AnimalShelter; +import com.avaje.tests.model.basic.BigDog; import com.avaje.tests.model.basic.Cat; import com.avaje.tests.model.basic.Dog; import org.junit.Test; @@ -18,31 +19,36 @@ public class TestInheritanceOnMany extends BaseTestCase { @Test public void test() { - + Cat cat = new Cat(); cat.setName("Puss"); Ebean.save(cat); - + Dog dog = new Dog(); dog.setRegistrationNumber("DOGGIE"); Ebean.save(dog); - + + BigDog bd = new BigDog(); + bd.setDogSize("large"); + bd.setRegistrationNumber("BG1"); + Ebean.save(bd); + AnimalShelter shelter = new AnimalShelter(); shelter.setName("My Animal Shelter"); shelter.getAnimals().add(cat); shelter.getAnimals().add(dog); - + Ebean.save(shelter); - + AnimalShelter shelter2 = Ebean.find(AnimalShelter.class, shelter.getId()); List animals = shelter2.getAnimals(); - + BeanList beanList = (BeanList)animals; ModifyListenMode modifyListenMode = beanList.getModifyListenMode(); - + 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 5c8f4ed80..942e023f8 100644 --- a/src/test/java/com/avaje/tests/model/basic/Animal.java +++ b/src/test/java/com/avaje/tests/model/basic/Animal.java @@ -7,11 +7,9 @@ import javax.persistence.Id; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.ManyToOne; -import javax.persistence.Table; import javax.persistence.Version; @Entity -@Table(name = "animals") @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name = "species") public abstract class Animal { diff --git a/src/test/java/com/avaje/tests/model/basic/BigDog.java b/src/test/java/com/avaje/tests/model/basic/BigDog.java new file mode 100644 index 000000000..162095940 --- /dev/null +++ b/src/test/java/com/avaje/tests/model/basic/BigDog.java @@ -0,0 +1,19 @@ +package com.avaje.tests.model.basic; + +import javax.persistence.DiscriminatorValue; +import javax.persistence.Entity; + +@Entity +@DiscriminatorValue("BDG") +public class BigDog extends Dog { + + String dogSize; + + public String getDogSize() { + return dogSize; + } + + public void setDogSize(String dogSize) { + this.dogSize = dogSize; + } +}