rewrite EntityBinder

This commit is contained in:
Gavin King 2022-11-02 13:56:31 +01:00
parent 3cd90d7615
commit bb0edab332
5 changed files with 499 additions and 635 deletions

View File

@ -448,8 +448,7 @@ public final class AnnotationBinder {
return null; return null;
} }
IdentifierGeneratorDefinition.Builder definitionBuilder = new IdentifierGeneratorDefinition.Builder(); final IdentifierGeneratorDefinition.Builder definitionBuilder = new IdentifierGeneratorDefinition.Builder();
if ( generatorAnn instanceof TableGenerator ) { if ( generatorAnn instanceof TableGenerator ) {
context.getBuildingOptions().getIdGenerationTypeInterpreter().interpretTableGenerator( context.getBuildingOptions().getIdGenerationTypeInterpreter().interpretTableGenerator(
(TableGenerator) generatorAnn, (TableGenerator) generatorAnn,
@ -469,11 +468,10 @@ public final class AnnotationBinder {
} }
} }
else if ( generatorAnn instanceof GenericGenerator ) { else if ( generatorAnn instanceof GenericGenerator ) {
GenericGenerator genGen = ( GenericGenerator ) generatorAnn; final GenericGenerator genGen = ( GenericGenerator ) generatorAnn;
definitionBuilder.setName( genGen.name() ); definitionBuilder.setName( genGen.name() );
definitionBuilder.setStrategy( genGen.strategy() ); definitionBuilder.setStrategy( genGen.strategy() );
Parameter[] params = genGen.parameters(); for ( Parameter parameter : genGen.parameters() ) {
for ( Parameter parameter : params ) {
definitionBuilder.addParam( parameter.name(), parameter.value() ); definitionBuilder.addParam( parameter.name(), parameter.value() );
} }
if ( LOG.isTraceEnabled() ) { if ( LOG.isTraceEnabled() ) {
@ -483,7 +481,6 @@ public final class AnnotationBinder {
else { else {
throw new AssertionFailure( "Unknown Generator annotation: " + generatorAnn ); throw new AssertionFailure( "Unknown Generator annotation: " + generatorAnn );
} }
return definitionBuilder.build(); return definitionBuilder.build();
} }
@ -514,7 +511,7 @@ public final class AnnotationBinder {
} }
// try to find class level generators // try to find class level generators
HashMap<String, IdentifierGeneratorDefinition> classGenerators = AnnotationBinder.buildGenerators( clazzToProcess, context ); final HashMap<String, IdentifierGeneratorDefinition> classGenerators = AnnotationBinder.buildGenerators( clazzToProcess, context );
handleTypeDescriptorRegistrations( clazzToProcess, context ); handleTypeDescriptorRegistrations( clazzToProcess, context );
bindEmbeddableInstantiatorRegistrations( clazzToProcess, context ); bindEmbeddableInstantiatorRegistrations( clazzToProcess, context );
bindCompositeUserTypeRegistrations( clazzToProcess, context ); bindCompositeUserTypeRegistrations( clazzToProcess, context );
@ -2347,7 +2344,7 @@ public final class AnnotationBinder {
//the classes are ordered thus preventing an NPE //the classes are ordered thus preventing an NPE
//FIXME if an entity has subclasses annotated @MappedSuperclass wo sub @Entity this is wrong //FIXME if an entity has subclasses annotated @MappedSuperclass wo sub @Entity this is wrong
superclassState.setHasSiblings( true ); superclassState.setHasSiblings( true );
InheritanceState superEntityState = getInheritanceStateOfSuperEntity( clazz, inheritanceStatePerClass ); final InheritanceState superEntityState = getInheritanceStateOfSuperEntity( clazz, inheritanceStatePerClass );
state.setHasParents( superEntityState != null ); state.setHasParents( superEntityState != null );
logMixedInheritance( clazz, superclassState, state ); logMixedInheritance( clazz, superclassState, state );
if ( superclassState.getType() != null ) { if ( superclassState.getType() != null ) {

View File

@ -83,13 +83,11 @@ public class InheritanceState {
return hasParents() && InheritanceType.TABLE_PER_CLASS.equals( getType() ); return hasParents() && InheritanceType.TABLE_PER_CLASS.equals( getType() );
} }
public static InheritanceState getInheritanceStateOfSuperEntity( public static InheritanceState getInheritanceStateOfSuperEntity(XClass clazz, Map<XClass, InheritanceState> states) {
XClass clazz, Map<XClass, InheritanceState> states
) {
XClass superclass = clazz; XClass superclass = clazz;
do { do {
superclass = superclass.getSuperclass(); superclass = superclass.getSuperclass();
InheritanceState currentState = states.get( superclass ); final InheritanceState currentState = states.get( superclass );
if ( currentState != null && !currentState.isEmbeddableSuperclass() ) { if ( currentState != null && !currentState.isEmbeddableSuperclass() ) {
return currentState; return currentState;
} }
@ -151,18 +149,19 @@ public class InheritanceState {
isEmbeddableSuperclass = embeddableSuperclass; isEmbeddableSuperclass = embeddableSuperclass;
} }
public void postProcess(PersistentClass persistenceClass, EntityBinder entityBinder) { public ElementsToProcess postProcess(PersistentClass persistenceClass, EntityBinder entityBinder) {
//make sure we run elements to process //make sure we run elements to process
getElementsToProcess(); getElementsToProcess();
addMappedSuperClassInMetadata( persistenceClass ); addMappedSuperClassInMetadata( persistenceClass );
entityBinder.setPropertyAccessType( accessType ); entityBinder.setPropertyAccessType( accessType );
return elementsToProcess;
} }
public XClass getClassWithIdClass(boolean evenIfSubclass) { public XClass getClassWithIdClass(boolean evenIfSubclass) {
if ( !evenIfSubclass && hasParents() ) { if ( !evenIfSubclass && hasParents() ) {
return null; return null;
} }
if ( clazz.isAnnotationPresent( IdClass.class ) ) { else if ( clazz.isAnnotationPresent( IdClass.class ) ) {
return clazz; return clazz;
} }
else { else {
@ -200,7 +199,7 @@ public class InheritanceState {
* guessing from @Id or @EmbeddedId presence if not specified. * guessing from @Id or @EmbeddedId presence if not specified.
* Change EntityBinder by side effect * Change EntityBinder by side effect
*/ */
public ElementsToProcess getElementsToProcess() { private ElementsToProcess getElementsToProcess() {
if ( elementsToProcess == null ) { if ( elementsToProcess == null ) {
InheritanceState inheritanceState = inheritanceStatePerClass.get( clazz ); InheritanceState inheritanceState = inheritanceStatePerClass.get( clazz );
assert !inheritanceState.isEmbeddableSuperclass(); assert !inheritanceState.isEmbeddableSuperclass();
@ -274,7 +273,6 @@ public class InheritanceState {
} }
private void getMappedSuperclassesTillNextEntityOrdered() { private void getMappedSuperclassesTillNextEntityOrdered() {
//ordered to allow proper messages on properties subclassing //ordered to allow proper messages on properties subclassing
XClass currentClassInHierarchy = clazz; XClass currentClassInHierarchy = clazz;
InheritanceState superclassState; InheritanceState superclassState;
@ -297,14 +295,13 @@ public class InheritanceState {
private void addMappedSuperClassInMetadata(PersistentClass persistentClass) { private void addMappedSuperClassInMetadata(PersistentClass persistentClass) {
//add @MappedSuperclass in the metadata //add @MappedSuperclass in the metadata
// classes from 0 to n-1 are @MappedSuperclass and should be linked // classes from 0 to n-1 are @MappedSuperclass and should be linked
org.hibernate.mapping.MappedSuperclass mappedSuperclass = null; final InheritanceState superEntityState = getInheritanceStateOfSuperEntity( clazz, inheritanceStatePerClass );
final InheritanceState superEntityState = final PersistentClass superEntity =
InheritanceState.getInheritanceStateOfSuperEntity( clazz, inheritanceStatePerClass );
PersistentClass superEntity =
superEntityState != null ? superEntityState != null ?
buildingContext.getMetadataCollector().getEntityBinding( superEntityState.getClazz().getName() ) : buildingContext.getMetadataCollector().getEntityBinding( superEntityState.getClazz().getName() ) :
null; null;
final int lastMappedSuperclass = classesToProcessForMappedSuperclass.size() - 1; final int lastMappedSuperclass = classesToProcessForMappedSuperclass.size() - 1;
org.hibernate.mapping.MappedSuperclass mappedSuperclass = null;
for ( int index = 0; index < lastMappedSuperclass; index++ ) { for ( int index = 0; index < lastMappedSuperclass; index++ ) {
org.hibernate.mapping.MappedSuperclass parentSuperclass = mappedSuperclass; org.hibernate.mapping.MappedSuperclass parentSuperclass = mappedSuperclass;
final Class<?> type = buildingContext.getBootstrapContext().getReflectionManager() final Class<?> type = buildingContext.getBootstrapContext().getReflectionManager()

View File

@ -168,8 +168,10 @@ public final class CallbackDefinitionResolverLegacyImpl {
return callbackDefinitions; return callbackDefinitions;
} }
public static List<CallbackDefinition> resolveEmbeddableCallbacks(ReflectionManager reflectionManager, public static List<CallbackDefinition> resolveEmbeddableCallbacks(
Class<?> entityClass, Property embeddableProperty, ReflectionManager reflectionManager,
Class<?> entityClass,
Property embeddableProperty,
CallbackType callbackType) { CallbackType callbackType) {
final Class<?> embeddableClass = embeddableProperty.getType().getReturnedClass(); final Class<?> embeddableClass = embeddableProperty.getType().getReturnedClass();
final XClass embeddableXClass = reflectionManager.toXClass( embeddableClass ); final XClass embeddableXClass = reflectionManager.toXClass( embeddableClass );

View File

@ -148,7 +148,8 @@ public abstract class AbstractIdentifiableType<J>
private void checkType(SingularPersistentAttribute<?, ?> attribute, Class<?> javaType) { private void checkType(SingularPersistentAttribute<?, ?> attribute, Class<?> javaType) {
if ( !javaType.isAssignableFrom( attribute.getType().getJavaType() ) ) { if ( !javaType.isAssignableFrom( attribute.getType().getJavaType() ) ) {
final JavaType<?> attributeJavaType = attribute.getAttributeJavaType(); final JavaType<?> attributeJavaType = attribute.getAttributeJavaType();
if ( !( attributeJavaType instanceof PrimitiveJavaType ) || ( (PrimitiveJavaType) attributeJavaType ).getPrimitiveClass() != javaType ) { if ( !( attributeJavaType instanceof PrimitiveJavaType )
|| ( (PrimitiveJavaType) attributeJavaType ).getPrimitiveClass() != javaType ) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
String.format( String.format(
"Attribute [%s#%s : %s] not castable to requested type [%s]", "Attribute [%s#%s : %s] not castable to requested type [%s]",