HHH-12654 - Throw MappingException if both @Inheritance and @AttributeOverride are used

This commit is contained in:
Vlad Mihalcea 2018-06-20 17:03:04 +03:00
parent f2238ec089
commit ebca36a768
2 changed files with 27 additions and 31 deletions

View File

@ -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(

View File

@ -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")