HHH-17833 turn two more warnings into exceptions

This commit is contained in:
Gavin King 2024-03-13 18:32:02 +01:00
parent 2f746f8004
commit d95084a59a
4 changed files with 16 additions and 128 deletions

View File

@ -70,6 +70,7 @@ import jakarta.persistence.TableGenerators;
import static org.hibernate.boot.model.internal.AnnotatedClassType.ENTITY;
import static org.hibernate.boot.model.internal.FilterDefBinder.bindFilterDefs;
import static org.hibernate.boot.model.internal.GeneratorBinder.buildGenerators;
import static org.hibernate.boot.model.internal.GeneratorBinder.buildIdGenerator;
import static org.hibernate.boot.model.internal.InheritanceState.getInheritanceStateOfSuperEntity;
import static org.hibernate.boot.model.internal.InheritanceState.getSuperclassInheritanceState;
import static org.hibernate.internal.CoreLogging.messageLogger;
@ -99,7 +100,7 @@ public final class AnnotationBinder {
List<SequenceGenerator> generators = ( List<SequenceGenerator> ) defaults.get( SequenceGenerator.class );
if ( generators != null ) {
for ( SequenceGenerator sequenceGenerator : generators ) {
final IdentifierGeneratorDefinition idGen = GeneratorBinder.buildIdGenerator( sequenceGenerator, context );
final IdentifierGeneratorDefinition idGen = buildIdGenerator( sequenceGenerator, context );
if ( idGen != null ) {
context.getMetadataCollector().addDefaultIdentifierGenerator( idGen );
}
@ -111,7 +112,7 @@ public final class AnnotationBinder {
List<TableGenerator> generators = ( List<TableGenerator> ) defaults.get( TableGenerator.class );
if ( generators != null ) {
for ( TableGenerator tableGenerator : generators ) {
final IdentifierGeneratorDefinition idGen = GeneratorBinder.buildIdGenerator( tableGenerator, context );
final IdentifierGeneratorDefinition idGen = buildIdGenerator( tableGenerator, context );
if ( idGen != null ) {
context.getMetadataCollector().addDefaultIdentifierGenerator( idGen );
}
@ -125,7 +126,7 @@ public final class AnnotationBinder {
if ( generators != null ) {
generators.forEach( tableGenerators -> {
for ( TableGenerator tableGenerator : tableGenerators.value() ) {
final IdentifierGeneratorDefinition idGen = GeneratorBinder.buildIdGenerator( tableGenerator, context );
final IdentifierGeneratorDefinition idGen = buildIdGenerator( tableGenerator, context );
if ( idGen != null ) {
context.getMetadataCollector().addDefaultIdentifierGenerator( idGen );
}
@ -140,7 +141,7 @@ public final class AnnotationBinder {
if ( generators != null ) {
generators.forEach( sequenceGenerators -> {
for ( SequenceGenerator sequenceGenerator : sequenceGenerators.value() ) {
final IdentifierGeneratorDefinition idGen = GeneratorBinder.buildIdGenerator( sequenceGenerator, context );
final IdentifierGeneratorDefinition idGen = buildIdGenerator( sequenceGenerator, context );
if ( idGen != null ) {
context.getMetadataCollector().addDefaultIdentifierGenerator( idGen );
}
@ -229,7 +230,7 @@ public final class AnnotationBinder {
private static void handleIdGenerators(XPackage annotatedPackage, MetadataBuildingContext context) {
if ( annotatedPackage.isAnnotationPresent( SequenceGenerator.class ) ) {
final SequenceGenerator sequenceGenerator = annotatedPackage.getAnnotation( SequenceGenerator.class );
IdentifierGeneratorDefinition idGen = GeneratorBinder.buildIdGenerator( sequenceGenerator, context );
IdentifierGeneratorDefinition idGen = buildIdGenerator( sequenceGenerator, context );
context.getMetadataCollector().addIdentifierGenerator( idGen );
if ( LOG.isTraceEnabled() ) {
LOG.tracev( "Add sequence generator with name: {0}", idGen.getName() );
@ -238,19 +239,19 @@ public final class AnnotationBinder {
if ( annotatedPackage.isAnnotationPresent( SequenceGenerators.class ) ) {
final SequenceGenerators sequenceGenerators = annotatedPackage.getAnnotation( SequenceGenerators.class );
for ( SequenceGenerator tableGenerator : sequenceGenerators.value() ) {
context.getMetadataCollector().addIdentifierGenerator( GeneratorBinder.buildIdGenerator( tableGenerator, context ) );
context.getMetadataCollector().addIdentifierGenerator( buildIdGenerator( tableGenerator, context ) );
}
}
if ( annotatedPackage.isAnnotationPresent( TableGenerator.class ) ) {
final TableGenerator tableGenerator = annotatedPackage.getAnnotation( TableGenerator.class );
IdentifierGeneratorDefinition idGen = GeneratorBinder.buildIdGenerator( tableGenerator, context );
IdentifierGeneratorDefinition idGen = buildIdGenerator( tableGenerator, context );
context.getMetadataCollector().addIdentifierGenerator( idGen );
}
if ( annotatedPackage.isAnnotationPresent( TableGenerators.class ) ) {
final TableGenerators tableGenerators = annotatedPackage.getAnnotation( TableGenerators.class );
for ( TableGenerator tableGenerator : tableGenerators.value() ) {
context.getMetadataCollector().addIdentifierGenerator( GeneratorBinder.buildIdGenerator( tableGenerator, context ) );
context.getMetadataCollector().addIdentifierGenerator( buildIdGenerator( tableGenerator, context ) );
}
}
}
@ -269,7 +270,7 @@ public final class AnnotationBinder {
}
private static void bindGenericGenerator(GenericGenerator def, MetadataBuildingContext context) {
context.getMetadataCollector().addIdentifierGenerator( GeneratorBinder.buildIdGenerator( def, context ) );
context.getMetadataCollector().addIdentifierGenerator( buildIdGenerator( def, context ) );
}
private static void bindNamedJpaQueries(XAnnotatedElement annotatedElement, MetadataBuildingContext context) {
@ -420,7 +421,8 @@ public final class AnnotationBinder {
+ "' may not specify a '@Table'" );
}
if ( annotatedClass.isAnnotationPresent( Inheritance.class ) ) {
LOG.unsupportedMappedSuperclassWithEntityInheritance( annotatedClass.getName() );
throw new AnnotationException( "Mapped superclass '" + annotatedClass.getName()
+ "' may not specify an '@Inheritance' mapping strategy" );
}
}
}
@ -716,8 +718,10 @@ public final class AnnotationBinder {
final boolean nonDefault = InheritanceType.SINGLE_TABLE != state.getType();
final boolean mixingStrategy = state.getType() != superclassState.getType();
if ( nonDefault && mixingStrategy ) {
//TODO: why on earth is this not an error!
LOG.invalidSubStrategy( clazz.getName() );
throw new AnnotationException( "Entity '" + clazz.getName()
+ "' may not override the inheritance mapping strategy '" + superclassState.getType()
+ "' of its hierarchy"
+ "' (each entity hierarchy has a single inheritance mapping strategy)" );
}
}
}

View File

@ -418,11 +418,6 @@ public interface CoreMessageLogger extends BasicLogger {
String name,
@Cause JndiNameException e);
@LogMessage(level = WARN)
@Message(value = "Mixing inheritance strategy in a entity hierarchy is not allowed, ignoring sub strategy in: %s",
id = 138)
void invalidSubStrategy(String className);
@LogMessage(level = INFO)
@Message(value = "JACC contextID: %s", id = 140)
void jaccContextId(String contextId);
@ -1765,10 +1760,6 @@ public interface CoreMessageLogger extends BasicLogger {
@Message(value = "The [%s] property of the [%s] entity was modified, but it won't be updated because the property is immutable.", id = 502)
void ignoreImmutablePropertyModification(String propertyName, String entityName);
@LogMessage(level = WARN)
@Message(value = "A class should not be annotated with both @Inheritance and @MappedSuperclass. @Inheritance will be ignored for: %s.", id = 503)
void unsupportedMappedSuperclassWithEntityInheritance(String entityName);
@LogMessage(level = WARN)
@Message(value = "Multiple configuration properties defined to create schema. Choose at most one among 'jakarta.persistence.create-database-schemas' or 'hibernate.hbm2ddl.create_namespaces'.", id = 504)
void multipleSchemaCreationSettingsDefined();

View File

@ -1,106 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.orm.test.inheritance;
import org.hibernate.boot.model.internal.AnnotationBinder;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase;
import org.hibernate.query.SemanticException;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.logger.LoggerInspectionRule;
import org.hibernate.testing.logger.Triggerable;
import org.hibernate.testing.util.ExceptionUtil;
import org.junit.Rule;
import org.junit.Test;
import org.jboss.logging.Logger;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Inheritance;
import jakarta.persistence.InheritanceType;
import jakarta.persistence.MappedSuperclass;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
*
* @author Vlad Mihalcea
*/
@TestForIssue(jiraKey = "HHH-12653")
public class MappedSuperclassInheritanceTest extends BaseEntityManagerFunctionalTestCase {
@Rule
public LoggerInspectionRule logInspection = new LoggerInspectionRule( Logger.getMessageLogger( CoreMessageLogger.class, AnnotationBinder.class.getName() ) );
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] {
Employee.class,
Manager.class,
Developer.class
};
}
@Override
public void buildEntityManagerFactory() {
Triggerable triggerable = logInspection.watchForLogMessages( "HHH000503:" );
triggerable.reset();
assertFalse( triggerable.wasTriggered() );
super.buildEntityManagerFactory();
assertTrue( triggerable.wasTriggered() );
assertTrue( triggerable.triggerMessage().contains( "A class should not be annotated with both @Inheritance and @MappedSuperclass. @Inheritance will be ignored for" ) );
}
@Test
public void test() {
doInJPA( this::entityManagerFactory, entityManager -> {
entityManager.createQuery("from Manager").getResultList();
entityManager.createQuery("from Developer").getResultList();
try {
//Check the @Inheritance annotation was ignored
entityManager.createQuery("from Employee").getResultList();
fail();
} catch (Exception expected) {
SemanticException rootException = (SemanticException) ExceptionUtil.rootCause( expected);
assertEquals("Could not resolve root entity 'Employee'", rootException.getMessage());
}
} );
}
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@MappedSuperclass
public static class Employee {
@Id
@GeneratedValue
private Long id;
private String jobType;
private String firstName;
private String lastName;
}
@Entity(name = "Manager")
public static class Manager extends Employee {
}
@Entity(name = "Developer")
public static class Developer extends Employee {
}
}

View File

@ -13,7 +13,6 @@ import jakarta.persistence.*;
import jakarta.persistence.Entity;
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class SubEntity extends SuperEntity {
@ManyToOne(fetch = FetchType.LAZY)