Skip to content

Commit

Permalink
drop arbitrary restrictions on what annotations can go where
Browse files Browse the repository at this point in the history
also add an error for competing @FilterDefs
  • Loading branch information
gavinking committed Dec 26, 2022
1 parent 491b1bc commit 169b9a8
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 50 deletions.
Expand Up @@ -147,6 +147,7 @@
import org.hibernate.usertype.internal.OffsetDateTimeCompositeUserType;
import org.hibernate.usertype.internal.ZonedDateTimeCompositeUserType;

import static org.hibernate.boot.model.internal.AnnotatedClassType.ENTITY;
import static org.hibernate.boot.model.internal.BinderHelper.getMappedSuperclassOrNull;
import static org.hibernate.boot.model.internal.BinderHelper.getOverridableAnnotation;
import static org.hibernate.boot.model.internal.BinderHelper.getPath;
Expand Down Expand Up @@ -305,11 +306,11 @@ public static void bindPackage(ClassLoaderService cls, String packageName, Metad

handleIdGenerators( annotatedPackage, context );

handleTypeDescriptorRegistrations( annotatedPackage, context );
bindTypeDescriptorRegistrations( annotatedPackage, context );
bindEmbeddableInstantiatorRegistrations( annotatedPackage, context );
bindUserTypeRegistrations( annotatedPackage, context );
bindCompositeUserTypeRegistrations( annotatedPackage, context );
handleConverterRegistrations( annotatedPackage, context );
bindConverterRegistrations( annotatedPackage, context );

bindGenericGenerators( annotatedPackage, context );
bindQueries( annotatedPackage, context );
Expand Down Expand Up @@ -520,31 +521,20 @@ public static void bindClass(

detectMappedSuperclassProblems( annotatedClass );

switch ( context.getMetadataCollector().getClassType( annotatedClass ) ) {
case MAPPED_SUPERCLASS:
// Allow queries and filters to be declared by a @MappedSuperclass
bindQueries( annotatedClass, context );
bindFilterDefs( annotatedClass, context );
//fall through:
case IMPORTED:
handleImport( annotatedClass, context );
case EMBEDDABLE:
case NONE:
return;
}

// try to find class level generators
final Map<String, IdentifierGeneratorDefinition> generators = buildGenerators( annotatedClass, context );
handleTypeDescriptorRegistrations( annotatedClass, context );
bindQueries( annotatedClass, context );
handleImport( annotatedClass, context );
bindFilterDefs( annotatedClass, context );
bindTypeDescriptorRegistrations( annotatedClass, context );
bindEmbeddableInstantiatorRegistrations( annotatedClass, context );
bindUserTypeRegistrations( annotatedClass, context );
bindCompositeUserTypeRegistrations( annotatedClass, context );
handleConverterRegistrations( annotatedClass, context );

bindQueries( annotatedClass, context );
bindFilterDefs( annotatedClass, context );
bindConverterRegistrations( annotatedClass, context );

EntityBinder.bindEntityClass( annotatedClass, inheritanceStatePerClass, generators, context );
// try to find class level generators
final Map<String, IdentifierGeneratorDefinition> generators = buildGenerators( annotatedClass, context );
if ( context.getMetadataCollector().getClassType( annotatedClass ) == ENTITY ) {
EntityBinder.bindEntityClass( annotatedClass, inheritanceStatePerClass, generators, context );
}
}

private static void handleImport(XClass annotatedClass, MetadataBuildingContext context) {
Expand All @@ -570,7 +560,7 @@ private static void detectMappedSuperclassProblems(XClass annotatedClass) {
}
}

private static void handleTypeDescriptorRegistrations(XAnnotatedElement annotatedElement, MetadataBuildingContext context) {
private static void bindTypeDescriptorRegistrations(XAnnotatedElement annotatedElement, MetadataBuildingContext context) {
final ManagedBeanRegistry managedBeanRegistry = context.getBootstrapContext()
.getServiceRegistry()
.getService( ManagedBeanRegistry.class );
Expand Down Expand Up @@ -726,7 +716,7 @@ private static void handleCompositeUserTypeRegistration(
);
}

private static void handleConverterRegistrations(XAnnotatedElement container, MetadataBuildingContext context) {
private static void bindConverterRegistrations(XAnnotatedElement container, MetadataBuildingContext context) {
final ConverterRegistration converterRegistration = container.getAnnotation( ConverterRegistration.class );
if ( converterRegistration != null ) {
handleConverterRegistration( converterRegistration, context );
Expand Down Expand Up @@ -769,6 +759,10 @@ public static void bindFilterDefs(XAnnotatedElement annotatedElement, MetadataBu

private static void bindFilterDef(FilterDef filterDef, MetadataBuildingContext context) {
final Map<String, JdbcMapping> explicitParamJaMappings = filterDef.parameters().length == 0 ? null : new HashMap<>();
final String name = filterDef.name();
if ( context.getMetadataCollector().getFilterDefinition( name ) != null ) {
throw new AnnotationException( "Multiple '@FilterDef' annotations define a filter named '" + name + "'" );
}
for ( ParamDef paramDef : filterDef.parameters() ) {
final JdbcMapping jdbcMapping = resolveFilterParamType( paramDef.type(), context );
if ( jdbcMapping == null ) {
Expand All @@ -777,14 +771,14 @@ private static void bindFilterDef(FilterDef filterDef, MetadataBuildingContext c
Locale.ROOT,
"Unable to resolve type specified for parameter (%s) defined for @FilterDef (%s)",
paramDef.name(),
filterDef.name()
name
)
);
}
explicitParamJaMappings.put( paramDef.name(), jdbcMapping );
}
final FilterDefinition filterDefinition =
new FilterDefinition( filterDef.name(), filterDef.defaultCondition(), explicitParamJaMappings );
new FilterDefinition(name, filterDef.defaultCondition(), explicitParamJaMappings );
LOG.debugf( "Binding filter definition: %s", filterDefinition.getFilterName() );
context.getMetadataCollector().addFilterDefinition( filterDefinition );
}
Expand Down Expand Up @@ -2484,7 +2478,6 @@ public static Map<XClass, InheritanceState> buildInheritanceStates(
final InheritanceState state = new InheritanceState( clazz, inheritanceStatePerClass, buildingContext );
if ( superclassState != null ) {
//the classes are ordered thus preventing an NPE
//FIXME if an entity has subclasses annotated @MappedSuperclass wo sub @Entity this is wrong
superclassState.setHasSiblings( true );
final InheritanceState superEntityState = getInheritanceStateOfSuperEntity( clazz, inheritanceStatePerClass );
state.setHasParents( superEntityState != null );
Expand All @@ -2493,7 +2486,12 @@ public static Map<XClass, InheritanceState> buildInheritanceStates(
state.setType( superclassState.getType() );
}
}
inheritanceStatePerClass.put( clazz, state );
switch ( buildingContext.getMetadataCollector().getClassType( clazz ) ) {
case ENTITY:
case MAPPED_SUPERCLASS:
case EMBEDDABLE:
inheritanceStatePerClass.put( clazz, state );
}
}
return inheritanceStatePerClass;
}
Expand Down
Expand Up @@ -137,6 +137,8 @@
import static jakarta.persistence.AccessType.PROPERTY;
import static jakarta.persistence.FetchType.EAGER;
import static jakarta.persistence.FetchType.LAZY;
import static org.hibernate.boot.model.internal.AnnotatedClassType.EMBEDDABLE;
import static org.hibernate.boot.model.internal.AnnotatedClassType.NONE;
import static org.hibernate.boot.model.internal.AnnotatedColumn.buildColumnFromAnnotation;
import static org.hibernate.boot.model.internal.AnnotatedColumn.buildColumnFromNoAnnotation;
import static org.hibernate.boot.model.internal.AnnotatedColumn.buildColumnsFromAnnotations;
Expand Down Expand Up @@ -2126,7 +2128,7 @@ private void handleElementCollection(

final XClass elementClass = isPrimitive( elementType.getName() ) ? null : elementType;
final AnnotatedClassType classType = annotatedElementType( isEmbedded, property, elementType );
final boolean primitive = classType == AnnotatedClassType.NONE;
final boolean primitive = classType == NONE;
if ( !primitive ) {
parentPropertyHolder.startingProperty( property );
}
Expand All @@ -2143,7 +2145,7 @@ private void handleElementCollection(

final Class<? extends CompositeUserType<?>> compositeUserType =
resolveCompositeUserType( property, elementClass, buildingContext );
if ( classType == AnnotatedClassType.EMBEDDABLE || compositeUserType != null ) {
if ( classType == EMBEDDABLE || compositeUserType != null ) {
handleCompositeCollectionElement( collection, property, hqlOrderBy, elementClass, holder, compositeUserType );
}
else {
Expand Down Expand Up @@ -2248,15 +2250,15 @@ AnnotatedClassType annotatedElementType(
XProperty property,
XClass elementType) {
if ( isPrimitive( elementType.getName() ) ) {
return AnnotatedClassType.NONE;
return NONE;
}
else {
//force in case of attribute override
final boolean attributeOverride = property.isAnnotationPresent( AttributeOverride.class )
|| property.isAnnotationPresent( AttributeOverrides.class );
// todo : force in the case of Convert annotation(s) with embedded paths (beyond key/value prefixes)?
return isEmbedded || attributeOverride
? AnnotatedClassType.EMBEDDABLE
? EMBEDDABLE
: buildingContext.getMetadataCollector().getClassType( elementType );
}
}
Expand Down
Expand Up @@ -121,6 +121,7 @@
import jakarta.persistence.SharedCacheMode;
import jakarta.persistence.UniqueConstraint;

import static org.hibernate.boot.model.internal.AnnotatedClassType.MAPPED_SUPERCLASS;
import static org.hibernate.boot.model.internal.AnnotatedDiscriminatorColumn.buildDiscriminatorColumn;
import static org.hibernate.boot.model.internal.AnnotatedJoinColumn.buildInheritanceJoinColumn;
import static org.hibernate.boot.model.internal.BinderHelper.getMappedSuperclassOrNull;
Expand Down Expand Up @@ -2100,7 +2101,7 @@ public void processComplementaryTableDefinitions(jakarta.persistence.Table table

public void processComplementaryTableDefinitions(org.hibernate.annotations.Table table) {
if ( table != null ) {
Table appliedTable = findTable( table.appliesTo() );
final Table appliedTable = findTable( table.appliesTo() );
if ( !table.comment().isEmpty() ) {
appliedTable.setComment( table.comment() );
}
Expand Down Expand Up @@ -2141,11 +2142,11 @@ public AccessType getPropertyAccessType() {
return propertyAccessType;
}

public void setPropertyAccessType(AccessType propertyAccessor) {
public void setPropertyAccessType(AccessType propertyAccessType) {
this.propertyAccessType = getExplicitAccessType( annotatedClass );
// only set the access type if there is no explicit access type for this class
if( this.propertyAccessType == null ) {
this.propertyAccessType = propertyAccessor;
if ( this.propertyAccessType == null ) {
this.propertyAccessType = propertyAccessType;
}
}

Expand Down Expand Up @@ -2174,7 +2175,7 @@ public void bindFiltersInHierarchy() {
XClass classToProcess = annotatedClass.getSuperclass();
while ( classToProcess != null ) {
final AnnotatedClassType classType = context.getMetadataCollector().getClassType( classToProcess );
if ( classType == AnnotatedClassType.MAPPED_SUPERCLASS ) {
if ( classType == MAPPED_SUPERCLASS ) {
bindFilters( classToProcess );
}
else {
Expand Down
Expand Up @@ -50,6 +50,7 @@
import jakarta.persistence.MapKeyJoinColumns;

import static org.hibernate.boot.model.internal.AnnotatedClassType.EMBEDDABLE;
import static org.hibernate.boot.model.internal.AnnotatedClassType.NONE;
import static org.hibernate.boot.model.internal.BinderHelper.findPropertyByName;
import static org.hibernate.boot.model.internal.BinderHelper.isPrimitive;
import static org.hibernate.boot.model.internal.PropertyHolderBuilder.buildPropertyHolder;
Expand Down Expand Up @@ -207,7 +208,7 @@ private AnnotatedClassType annotatedMapKeyType(
String mapKeyType,
XClass keyClass) {
if ( isPrimitive( mapKeyType ) ) {
return AnnotatedClassType.NONE;
return NONE;
}
else {
// force in case of attribute override naming the key
Expand Down
Expand Up @@ -12,7 +12,6 @@
import java.util.Map;
import java.util.Set;

import org.hibernate.annotations.Imported;
import org.hibernate.annotations.common.reflection.MetadataProviderInjector;
import org.hibernate.annotations.common.reflection.ReflectionManager;
import org.hibernate.annotations.common.reflection.XClass;
Expand All @@ -37,9 +36,7 @@

import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;
import jakarta.persistence.Embeddable;
import jakarta.persistence.Entity;
import jakarta.persistence.MappedSuperclass;

/**
* @author Steve Ebersole
Expand Down Expand Up @@ -114,14 +111,8 @@ private void categorizeAnnotatedClass(Class<?> annotatedClass, ConverterRegistry
//noinspection unchecked
converterRegistry.addAttributeConverter( (Class<? extends AttributeConverter<?,?>>) annotatedClass );
}
else if ( xClass.isAnnotationPresent( Entity.class )
|| xClass.isAnnotationPresent( MappedSuperclass.class )
|| xClass.isAnnotationPresent( Embeddable.class )
|| xClass.isAnnotationPresent( Imported.class ) ) {
xClasses.add( xClass );
}
else {
log.debugf( "Encountered a non-categorized annotated class [%s]; ignoring", annotatedClass.getName() );
xClasses.add( xClass );
}
}

Expand Down
Expand Up @@ -27,7 +27,6 @@
*/
@Entity
@Table(name = "tbl_group")
@FilterDef(name="Groupfilter")
public class GroupWithSet {
private Integer id;
private Set<Permission> permissions;
Expand Down

0 comments on commit 169b9a8

Please sign in to comment.