Skip to content

Commit

Permalink
HHH-17504 - Ongoing JPA 32 work
Browse files Browse the repository at this point in the history
HHH-17350 - Work on hibernate-models, XSD and JAXB
HHH-16114 - Improve boot metamodel binding
HHH-15996 - Develop an abstraction for Annotation in annotation processing
HHH-16012 - Develop an abstraction for domain model Class refs
HHH-15997 - Support for dynamic models in orm.xml
HHH-15698 - Support for entity-name in mapping.xsd
  • Loading branch information
sebersole committed Dec 5, 2023
1 parent bb9b17c commit 307aa43
Show file tree
Hide file tree
Showing 10 changed files with 325 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,32 @@

import org.jboss.logging.BasicLogger;
import org.jboss.logging.Logger;
import org.jboss.logging.annotations.LogMessage;
import org.jboss.logging.annotations.Message;
import org.jboss.logging.annotations.MessageLogger;
import org.jboss.logging.annotations.ValidIdRange;

import static org.jboss.logging.Logger.Level.INFO;

/**
* todo : find the proper min/max id range
*
* @author Steve Ebersole
*/
@Internal
@MessageLogger( projectCode = "HHH" )
@ValidIdRange( min = 999980, max = 999999 )
public interface ModelBindingLogging extends BasicLogger {
String NAME = "org.hibernate.models.orm";

Logger MODEL_BINDING_LOGGER = Logger.getLogger( NAME );
ModelBindingLogging MODEL_BINDING_MSG_LOGGER = Logger.getMessageLogger( ModelBindingLogging.class, NAME );

@LogMessage(level = INFO)
@Message( id = 999980, value = "Entity `%s` used both @DynamicInsert and @SQLInsert" )
void dynamicAndCustomInsert(String entityName);

@LogMessage(level = INFO)
@Message( id = 999981, value = "Entity `%s` used both @DynamicUpdate and @SQLUpdate" )
void dynamicAndCustomUpdate(String entityName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import jakarta.persistence.Convert;

import static org.hibernate.boot.models.categorize.spi.AttributeMetadata.AttributeNature.BASIC;
import static org.hibernate.boot.models.categorize.spi.AttributeMetadata.AttributeNature.EMBEDDED;

/**
* Binding for an attribute
Expand All @@ -61,16 +62,21 @@ public AttributeBinding(
this.property = new Property();
this.property.setName( attributeMetadata.getName() );

final Value value;
if ( attributeMetadata.getNature() == BASIC ) {
final var basicValue = createBasicValue( primaryTable );
property.setValue( basicValue );
attributeTable = basicValue.getTable();
mappingValue = basicValue;
value = createBasicValue( primaryTable );
}
else if ( attributeMetadata.getNature() == EMBEDDED ) {
value = createComponentValue( primaryTable, owner );
}
else {
throw new UnsupportedOperationException( "Not yet implemented" );
}

property.setValue( value );
attributeTable = value.getTable();
mappingValue = value;

applyNaturalId( attributeMetadata, property );
}

Expand Down Expand Up @@ -132,6 +138,16 @@ private BasicValue createBasicValue(Table primaryTable) {
return basicValue;
}

private Component createComponentValue(Table primaryTable, PersistentClass persistentClass) {
final Component component = new Component( bindingState.getMetadataBuildingContext(), persistentClass );

// 1. embeddable (attributes, etc)
// 2. overrides
final MemberDetails member = attributeMetadata.getMember();

return component;
}

public Property getProperty() {
return property;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,85 +13,20 @@

import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.ObjectNameNormalizer;
import org.hibernate.boot.models.bind.spi.BindingContext;
import org.hibernate.boot.models.bind.spi.BindingOptions;
import org.hibernate.boot.models.bind.spi.BindingState;
import org.hibernate.boot.models.bind.spi.QuotedIdentifierTarget;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.models.ModelsException;
import org.hibernate.models.spi.AnnotationDescriptor;
import org.hibernate.models.spi.AnnotationUsage;
import org.hibernate.models.spi.AttributeDescriptor;

import static org.hibernate.boot.models.bind.ModelBindingLogging.MODEL_BINDING_LOGGER;

/**
* @author Steve Ebersole
*/
public class BindingHelper {
public static <T, A extends Annotation> T getValue(
AnnotationUsage<A> annotationUsage,
String attributeName,
Class<A> annotationType,
BindingContext context) {
final T valueOrNull = getValueOrNull( annotationUsage, attributeName );
if ( valueOrNull != null ) {
return valueOrNull;
}

// resolve its default
return getDefaultValue( attributeName, annotationType, context );
}

public static <T, A extends Annotation> T getValueOrNull(
AnnotationUsage<A> annotationUsage,
String attributeName) {
if ( annotationUsage != null ) {
// allow to return null if missing
return annotationUsage.getAttributeValue( attributeName );
}

// there was no annotation...
return null;
}

public static <T,A extends Annotation> T getDefaultValue(
String attributeName,
Class<A> annotationType,
BindingContext context) {
final AnnotationDescriptor<A> annotationDescriptor = context.getAnnotationDescriptorRegistry().getDescriptor( annotationType );
final AttributeDescriptor<Object> attributeDescriptor = annotationDescriptor.getAttribute( attributeName );
//noinspection unchecked
return (T) attributeDescriptor.getAttributeMethod().getDefaultValue();

}

public static <A extends Annotation> String getString(
AnnotationUsage<A> annotationUsage,
String attributeName,
Class<A> annotationType,
BindingContext context) {
return getValue( annotationUsage, attributeName, annotationType, context );
}

public static <A extends Annotation> String getStringOrNull(
AnnotationUsage<A> annotationUsage,
String attributeName) {
return getValueOrNull( annotationUsage, attributeName );
}

public static <A extends Annotation> Identifier getIdentifier(
AnnotationUsage<A> annotationUsage,
String attributeName,
Class<A> annotationType,
QuotedIdentifierTarget target,
BindingOptions options,
JdbcEnvironment jdbcEnvironment,
BindingContext context) {
final String name = getString( annotationUsage, attributeName, annotationType, context );
final boolean globallyQuoted = options.getGloballyQuotedIdentifierTargets().contains( target );
return jdbcEnvironment.getIdentifierHelper().toIdentifier( name, globallyQuoted );
}

public static <A extends Annotation> Identifier toIdentifier(
String name,
Expand All @@ -116,17 +51,12 @@ public static <T,A extends Annotation> T getValue(AnnotationUsage<A> ann, String
return (T) descriptor.getAttribute( attributeName ).getAttributeMethod().getDefaultValue();
}

//noinspection unchecked
return getValue(
ann,
attributeName,
() -> (T) descriptor.getAttribute( attributeName ).getAttributeMethod().getDefaultValue()
);
return ann.getAttributeValue( attributeName );
}

public static <T,A extends Annotation> T getValue(AnnotationUsage<A> ann, String attributeName, Supplier<T> defaultValueSupplier) {
if ( ann == null ) {
return (T) defaultValueSupplier.get();
return defaultValueSupplier.get();
}

return ann.getAttributeValue( attributeName, defaultValueSupplier );
Expand Down

0 comments on commit 307aa43

Please sign in to comment.