Skip to content

Commit

Permalink
HHH-17377 - Migrate to JPA 3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
sebersole committed Nov 17, 2023
1 parent bedad42 commit bf8dc9d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
* @author Steve Ebersole
*/
public class HbmTransformationLogging {
public static final String TRANSFORMATION_LOGGER_NAME = JaxbLogger.LOGGER_NAME + ".hbm-transformation";
public static final String TRANSFORMATION_LOGGER_NAME = JaxbLogger.LOGGER_NAME + ".hbm-transform";
public static final Logger TRANSFORMATION_LOGGER = Logger.getLogger( TRANSFORMATION_LOGGER_NAME );

public static final boolean TRACE_ENABLED = TRANSFORMATION_LOGGER.isTraceEnabled();
public static final boolean DEBUG_ENABLED = TRANSFORMATION_LOGGER.isDebugEnabled();
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmSimpleIdType;
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmSynchronizeType;
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmTimestampAttributeType;
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmTypeDefinitionType;
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmUnionSubclassEntityType;
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmVersionAttributeType;
import org.hibernate.boot.jaxb.hbm.spi.PluralAttributeInfo;
Expand Down Expand Up @@ -242,25 +243,33 @@ private void handleUnsupportedContent(String description) {
}

private void handleUnsupported(String message, Object... messageArgs) {
if ( options.unsupportedFeatureHandling() == UnsupportedFeatureHandling.ERROR ) {
throw new UnsupportedOperationException(
handleUnsupported(
null,
message,
messageArgs
);
}

@FunctionalInterface
private interface PickHandler {
void handlePick(String message, Object... messageArgs);
}

private void handleUnsupported(PickHandler pickHandler, String message, Object... messageArgs) {
switch ( options.unsupportedFeatureHandling() ) {
case ERROR -> throw new UnsupportedOperationException(
String.format(
Locale.ROOT,
message,
messageArgs
)
);
case PICK -> {
if ( pickHandler != null ) pickHandler.handlePick( message, messageArgs );
}
case IGNORE -> TRANSFORMATION_LOGGER.debugf( message, messageArgs );
case WARN -> TRANSFORMATION_LOGGER.warnf( message, messageArgs );
}

final Logger.Level logLevel = options.unsupportedFeatureHandling() == UnsupportedFeatureHandling.WARN
? Logger.Level.WARN
: Logger.Level.DEBUG;
//noinspection deprecation
TRANSFORMATION_LOGGER.log(
logLevel,
message,
messageArgs
);
}

private void transferTypeDefs() {
Expand Down Expand Up @@ -295,8 +304,6 @@ private void transferIdentifierGenerators() {
ormRoot.getGenericGenerators().add( generatorDef );
generatorDef.setName( hbmGenerator.getName() );
generatorDef.setClazz( hbmGenerator.getClazz() );

// todo : parameters
}
}

Expand Down Expand Up @@ -326,7 +333,7 @@ private void transferFilterDefinitions() {
for ( Object content : hbmFilterDef.getContent() ) {
if ( content instanceof String ) {
final String condition = ( (String) content ).trim();
if (! StringHelper.isEmpty( condition )) {
if ( !StringHelper.isEmpty( condition ) ) {
foundCondition = true;
filterDef.setDefaultCondition( condition );
}
Expand Down Expand Up @@ -575,10 +582,9 @@ private JaxbNamedQueryImpl transformNamedQuery(JaxbHbmNamedQueryType hbmQuery, S
query.setTimeout( hbmQuery.getTimeout() );

for ( Object content : hbmQuery.getContent() ) {
if ( content instanceof String ) {
String s = (String) content;
s = s.trim();
query.setQuery( s );
if ( content instanceof String qryString ) {
qryString = qryString.trim();
query.setQuery( qryString );
}
else {
@SuppressWarnings("unchecked") final JAXBElement<JaxbHbmQueryParamType> element = (JAXBElement<JaxbHbmQueryParamType>) content;
Expand Down Expand Up @@ -633,15 +639,13 @@ private JaxbNamedNativeQueryImpl transformNamedNativeQuery(JaxbHbmNamedNativeQue

// JaxbQueryElement#content elements can be either the query or parameters
for ( Object content : hbmQuery.getContent() ) {
if ( content instanceof String ) {
String s = (String) content;
s = s.trim();
query.setQuery( s );
if ( content instanceof String qryString ) {
qryString = qryString.trim();
query.setQuery( qryString );
}
else if ( content instanceof JAXBElement ) {
final Object element = ( (JAXBElement<?>) content ).getValue();
if ( element instanceof JaxbHbmQueryParamType ) {
final JaxbHbmQueryParamType hbmQueryParam = (JaxbHbmQueryParamType) element;
if ( element instanceof JaxbHbmQueryParamType hbmQueryParam ) {
final JaxbQueryParamTypeImpl queryParam = new JaxbQueryParamTypeImpl();
queryParam.setName( hbmQueryParam.getName() );
queryParam.setType( hbmQueryParam.getType() );
Expand Down Expand Up @@ -705,8 +709,7 @@ else if ( element instanceof JaxbHbmNativeQueryJoinReturnType ) {
)
);
}
else if ( element instanceof JaxbHbmSynchronizeType ) {
final JaxbHbmSynchronizeType hbmSynchronize = (JaxbHbmSynchronizeType) element;
else if ( element instanceof JaxbHbmSynchronizeType hbmSynchronize ) {
final JaxbSynchronizedTableImpl synchronize = new JaxbSynchronizedTableImpl();
synchronize.setTable( hbmSynchronize.getTable() );
query.getSynchronizations().add( synchronize );
Expand Down Expand Up @@ -761,7 +764,7 @@ private void transferDatabaseObjects() {
private void transferEntities() {
// thoughts...
// 1) We only need to transfer the "extends" attribute if the model is dynamic (map mode),
// otherwise it will be discovered via jandex
// otherwise it will be discovered via hibernate-models
// 2) ?? Have abstract hbm class mappings become MappedSuperclass mappings ??

for ( JaxbHbmRootEntityType hbmClass : hbmXmlMapping.getClazz() ) {
Expand Down Expand Up @@ -831,7 +834,10 @@ private void transferRootEntity(JaxbHbmRootEntityType hbmClass, JaxbEntityImpl e
}

if ( hbmClass.getLoader() != null ) {
throw new UnsupportedOperationException( "<loader/> is not supported in mapping.xsd - use <sql-select/> or <hql-select/> instead" );
handleUnsupported(
"<loader/> is not supported in mapping.xsd - use <sql-select/> or <hql-select/> instead: ",
origin
);
}

if ( hbmClass.getSqlInsert() != null ) {
Expand Down Expand Up @@ -964,6 +970,7 @@ private boolean convert(JaxbHbmCacheInclusionEnum hbmInclusion) {
throw new IllegalArgumentException( "Unrecognized cache-inclusions value : " + hbmInclusion );
}

@SuppressWarnings("deprecation")
private PolymorphismType convert(JaxbHbmPolymorphismEnum polymorphism) {
if ( polymorphism == null ) {
return null;
Expand All @@ -977,19 +984,13 @@ private void transferBaseEntityInformation(JaxbHbmEntityBaseDefinition hbmClass,
transfer( hbmClass::getEntityName, entity::setName );
transfer( hbmClass::getName, entity::setClazz );

if ( hbmClass instanceof Discriminatable ) {
final Discriminatable discriminatable = (Discriminatable) hbmClass;
if ( hbmClass instanceof Discriminatable discriminatable ) {
transfer( discriminatable::getDiscriminatorValue, entity::setDiscriminatorValue );
}

// todo (6.1) : what to do with abstract? add abstract attribute to mapping xsd, or handle as mapped-superclass?
if ( hbmClass.isAbstract() != null ) {
handleUnsupported(
"Transformation of abstract entity mappings is not supported : `%s` - `%s`",
extractEntityName( hbmClass ),
origin
);
return;
// todo : handle hbm abstract as mapping abstract or as mapped-superclass?
entity.setAbstract( hbmClass.isAbstract() );
}

if ( hbmClass.getPersister() != null ) {
Expand Down Expand Up @@ -1232,17 +1233,14 @@ private void transferEntityElementAttributes(EntityInfo hbmClass, JaxbEntityImpl

private void transferAttributes(List hbmAttributeMappings, JaxbAttributesContainer attributes) {
for ( Object hbmAttributeMapping : hbmAttributeMappings ) {
if ( hbmAttributeMapping instanceof JaxbHbmBasicAttributeType ) {
final JaxbHbmBasicAttributeType basic = (JaxbHbmBasicAttributeType) hbmAttributeMapping;
if ( hbmAttributeMapping instanceof JaxbHbmBasicAttributeType basic ) {
attributes.getBasicAttributes().add( transformBasicAttribute( basic ) );
}
else if ( hbmAttributeMapping instanceof JaxbHbmCompositeAttributeType ) {
final JaxbHbmCompositeAttributeType hbmComponent = (JaxbHbmCompositeAttributeType) hbmAttributeMapping;
else if ( hbmAttributeMapping instanceof JaxbHbmCompositeAttributeType hbmComponent ) {
ormRoot.getEmbeddables().add( convertEmbeddable( hbmComponent ) );
attributes.getEmbeddedAttributes().add( transformEmbedded( hbmComponent ) );
}
else if ( hbmAttributeMapping instanceof JaxbHbmPropertiesType ) {
final JaxbHbmPropertiesType hbmProperties = (JaxbHbmPropertiesType) hbmAttributeMapping;
else if ( hbmAttributeMapping instanceof JaxbHbmPropertiesType hbmProperties ) {
transferAttributes( hbmProperties.getAttributes(), attributes );
}
else if ( hbmAttributeMapping instanceof JaxbHbmDynamicComponentType ) {
Expand All @@ -1252,22 +1250,17 @@ else if ( hbmAttributeMapping instanceof JaxbHbmDynamicComponentType ) {
name
);
}
else if ( hbmAttributeMapping instanceof JaxbHbmOneToOneType ) {
final JaxbHbmOneToOneType o2o = (JaxbHbmOneToOneType) hbmAttributeMapping;
else if ( hbmAttributeMapping instanceof JaxbHbmOneToOneType o2o ) {
transferOneToOne( o2o, attributes );
}
else if ( hbmAttributeMapping instanceof JaxbHbmManyToOneType ) {
final JaxbHbmManyToOneType m2o = (JaxbHbmManyToOneType) hbmAttributeMapping;
else if ( hbmAttributeMapping instanceof JaxbHbmManyToOneType m2o ) {
attributes.getManyToOneAttributes().add( transformManyToOne( m2o ) );
}
else if ( hbmAttributeMapping instanceof JaxbHbmAnyAssociationType ) {
final JaxbHbmAnyAssociationType any = (JaxbHbmAnyAssociationType) hbmAttributeMapping;
else if ( hbmAttributeMapping instanceof JaxbHbmAnyAssociationType any ) {
attributes.getAnyMappingAttributes().add( transformAnyAttribute( any ) );

}
else if ( hbmAttributeMapping instanceof PluralAttributeInfo ) {
final PluralAttributeInfo pluralAttributeInfo = (PluralAttributeInfo) hbmAttributeMapping;

else if ( hbmAttributeMapping instanceof PluralAttributeInfo pluralAttributeInfo ) {
if ( pluralAttributeInfo.getElement() != null
|| pluralAttributeInfo.getCompositeElement() != null ) {
attributes.getElementCollectionAttributes().add( transformElementCollection( pluralAttributeInfo ) );
Expand Down Expand Up @@ -1632,7 +1625,7 @@ public String getFormulaAttribute() {

@Override
public List<Serializable> getColumnOrFormula() {
return new ArrayList<Serializable>( source.getKey().getColumn() );
return new ArrayList<>( source.getKey().getColumn() );
}

@Override
Expand Down Expand Up @@ -1678,13 +1671,11 @@ private void transferCollectionBasicInfo(PluralAttributeInfo source, JaxbPluralA
target.setAttributeAccessor( source.getAccess() );
target.setFetchMode( convert( source.getFetch() ) );

if ( source instanceof JaxbHbmSetType ) {
final JaxbHbmSetType set = (JaxbHbmSetType) source;
if ( source instanceof JaxbHbmSetType set ) {
target.setSort( set.getSort() );
target.setOrderBy( set.getOrderBy() );
}
else if ( source instanceof JaxbHbmMapType ) {
final JaxbHbmMapType map = (JaxbHbmMapType) source;
else if ( source instanceof JaxbHbmMapType map ) {
target.setSort( map.getSort() );
target.setOrderBy( map.getOrderBy() );
}
Expand Down Expand Up @@ -1781,17 +1772,11 @@ private Boolean invert(Boolean value, Boolean defaultValue) {

private JaxbPluralFetchModeImpl convert(JaxbHbmFetchStyleWithSubselectEnum fetch) {
if ( fetch != null ) {
switch ( fetch ) {
case SELECT: {
return JaxbPluralFetchModeImpl.SELECT;
}
case JOIN: {
return JaxbPluralFetchModeImpl.JOIN;
}
case SUBSELECT: {
return JaxbPluralFetchModeImpl.SUBSELECT;
}
}
return switch ( fetch ) {
case SELECT -> JaxbPluralFetchModeImpl.SELECT;
case JOIN -> JaxbPluralFetchModeImpl.JOIN;
case SUBSELECT -> JaxbPluralFetchModeImpl.SUBSELECT;
};
}

return null;
Expand Down Expand Up @@ -1820,13 +1805,8 @@ private void transferIdentifier(JaxbHbmRootEntityType source, JaxbEntityImpl tar
final boolean isAggregate;
if ( isNotEmpty( hbmCompositeId.getClazz() ) ) {
// we have <composite-id class="XYZ">.
if ( hbmCompositeId.isMapped() ) {
// user explicitly said the class is an "IdClass"
isAggregate = false;
}
else {
isAggregate = true;
}
// user explicitly said the class is an "IdClass"
isAggregate = !hbmCompositeId.isMapped();
}
else {
// there was no class specified, can only be non-aggregated
Expand All @@ -1842,8 +1822,7 @@ private void transferIdentifier(JaxbHbmRootEntityType source, JaxbEntityImpl tar
embeddable.setClazz( hbmCompositeId.getClazz() );
embeddable.setAttributes( new JaxbEmbeddableAttributesContainerImpl() );
for ( Object hbmCompositeAttribute : hbmCompositeId.getKeyPropertyOrKeyManyToOne() ) {
if ( hbmCompositeAttribute instanceof JaxbHbmCompositeKeyBasicAttributeType ) {
final JaxbHbmCompositeKeyBasicAttributeType keyProp = (JaxbHbmCompositeKeyBasicAttributeType) hbmCompositeAttribute;
if ( hbmCompositeAttribute instanceof JaxbHbmCompositeKeyBasicAttributeType keyProp ) {
final JaxbBasicImpl basic = new JaxbBasicImpl();
basic.setName( keyProp.getName() );
basic.setAttributeAccessor( keyProp.getAccess() );
Expand Down Expand Up @@ -1877,8 +1856,7 @@ private void transferIdentifier(JaxbHbmRootEntityType source, JaxbEntityImpl tar
idClass.setClazz( hbmCompositeId.getClazz() );
target.setIdClass( idClass );
for ( Object hbmCompositeAttribute : hbmCompositeId.getKeyPropertyOrKeyManyToOne() ) {
if ( hbmCompositeAttribute instanceof JaxbHbmCompositeKeyBasicAttributeType ) {
final JaxbHbmCompositeKeyBasicAttributeType keyProp = (JaxbHbmCompositeKeyBasicAttributeType) hbmCompositeAttribute;
if ( hbmCompositeAttribute instanceof JaxbHbmCompositeKeyBasicAttributeType keyProp ) {
final JaxbIdImpl id = new JaxbIdImpl();
id.setName( keyProp.getName() );
id.setAttributeAccessor( keyProp.getAccess() );
Expand Down Expand Up @@ -2055,6 +2033,7 @@ private void transferTimestamp(JaxbHbmRootEntityType source, JaxbEntityImpl targ
version.setColumn( new JaxbColumnImpl() );
version.getColumn().setName( hbmTimestamp.getColumnAttribute() );
}
//noinspection deprecation
version.setTemporal( TemporalType.TIMESTAMP );
target.getAttributes().getVersion().add( version );
}
Expand Down

This file was deleted.

0 comments on commit bf8dc9d

Please sign in to comment.