From ebca36a76851c9b0b7ea406e1b1428225891de2f Mon Sep 17 00:00:00 2001 From: Vlad Mihalcea Date: Wed, 20 Jun 2018 17:03:04 +0300 Subject: [PATCH] HHH-12654 - Throw MappingException if both @Inheritance and @AttributeOverride are used --- .../org/hibernate/cfg/AnnotationBinder.java | 11 +++++ ...ntityInheritanceAttributeOverrideTest.java | 47 +++++++------------ 2 files changed, 27 insertions(+), 31 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java index 35706763fc..5f42153717 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java @@ -19,6 +19,8 @@ import java.util.Locale; import java.util.Map; import java.util.Properties; import java.util.Set; +import javax.persistence.AttributeOverride; +import javax.persistence.AttributeOverrides; import javax.persistence.Basic; import javax.persistence.CollectionTable; import javax.persistence.Column; @@ -556,6 +558,15 @@ public final class AnnotationBinder { inheritanceState ); + if(superEntity != null && ( + clazzToProcess.getAnnotation( AttributeOverride.class ) != null || + clazzToProcess.getAnnotation( AttributeOverrides.class ) != null ) ) { + throw new AnnotationException( + "An entity annotated with @Inheritance cannot use @AttributeOverride or @AttributeOverrides: " + + clazzToProcess.getName() + ); + } + PersistentClass persistentClass = makePersistentClass( inheritanceState, superEntity, context ); Entity entityAnn = clazzToProcess.getAnnotation( Entity.class ); org.hibernate.annotations.Entity hibEntityAnn = clazzToProcess.getAnnotation( diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/override/inheritance/EntityInheritanceAttributeOverrideTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/override/inheritance/EntityInheritanceAttributeOverrideTest.java index e595637dc7..e39188d0b7 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/override/inheritance/EntityInheritanceAttributeOverrideTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/override/inheritance/EntityInheritanceAttributeOverrideTest.java @@ -16,6 +16,7 @@ import javax.persistence.MappedSuperclass; import javax.persistence.Table; import javax.persistence.UniqueConstraint; +import org.hibernate.AnnotationException; import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; import org.hibernate.testing.FailureExpected; @@ -24,11 +25,15 @@ import org.junit.Test; import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; /** * @author Vlad Mihalcea */ +@TestForIssue( jiraKey = "HHH-12609, HHH-12654" ) public class EntityInheritanceAttributeOverrideTest extends BaseEntityManagerFunctionalTestCase { + @Override public Class[] getAnnotatedClasses() { return new Class[]{ @@ -38,39 +43,19 @@ public class EntityInheritanceAttributeOverrideTest extends BaseEntityManagerFun }; } + @Override + public void buildEntityManagerFactory() { + try { + super.buildEntityManagerFactory(); + fail("Should throw AnnotationException"); + } + catch (AnnotationException e) { + assertTrue( e.getMessage().startsWith( "An entity annotated with @Inheritance cannot use @AttributeOverride or @AttributeOverrides" ) ); + } + } + @Test - @TestForIssue( jiraKey = "HHH-12609" ) - @FailureExpected( - jiraKey = "HHH-12609", - message = "Base class attributes cannot be overridden unless the base class is @MappedSuperclass" - ) public void test() { - doInJPA( this::entityManagerFactory, entityManager -> { - TaxonEntity taxon1 = new TaxonEntity(); - taxon1.setId( 1L ); - taxon1.setCode( "Taxon" ); - taxon1.setCatalogVersion( "C1" ); - - entityManager.persist( taxon1 ); - - TaxonEntity taxon2 = new TaxonEntity(); - taxon2.setId( 2L ); - taxon2.setCode( "Taxon" ); - taxon2.setCatalogVersion( "C2" ); - - entityManager.persist( taxon2 ); - } ); - doInJPA( this::entityManagerFactory, entityManager -> { - assertEquals( - 2, - ((Number) entityManager.createQuery( - "select count(t) " + - "from Taxon t " + - "where t.code = :code" ) - .setParameter( "code", "Taxon" ) - .getSingleResult()).intValue() - ); - } ); } @Entity(name = "AbstractEntity")