Skip to content

Commit

Permalink
#8 - Create AnnotationUsage for id-class
Browse files Browse the repository at this point in the history
  • Loading branch information
mbladel committed Oct 27, 2023
1 parent 89a2a01 commit d4de89b
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.hibernate.boot.jaxb.mapping.spi.JaxbEmbeddedImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappingsImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityOrMappedSuperclass;
import org.hibernate.boot.jaxb.mapping.spi.JaxbIdImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbManagedType;
import org.hibernate.boot.jaxb.mapping.spi.JaxbMappedSuperclassImpl;
Expand Down Expand Up @@ -454,7 +455,8 @@ private static void processEntityMetadata(
sourceModelBuildingContext
) );

// todo : id-class
processCommonEntityOrMappedSuperclass( jaxbEntity, classDetails, sourceModelBuildingContext );

// todo : callbacks
// todo : entity-listeners
// todo : secondary-tables
Expand Down Expand Up @@ -579,7 +581,8 @@ private static void processMappedSuperclassMetadata(
final JaxbAttributesContainer attributes = jaxbMappedSuperclass.getAttributes();
processAttributes( attributes, classDetails, classAccessType, sourceModelBuildingContext );

// todo : id-class
processCommonEntityOrMappedSuperclass( jaxbMappedSuperclass, classDetails, sourceModelBuildingContext );

// todo : entity-listeners
// todo : callbacks
}
Expand All @@ -600,6 +603,17 @@ public static void processOverrideMappedSuperclass(
} );
}

private static void processCommonEntityOrMappedSuperclass(
JaxbEntityOrMappedSuperclass jaxbEntity,
MutableClassDetails classDetails,
SourceModelBuildingContext sourceModelBuildingContext) {
XmlAnnotationHelper.applyIdClass( jaxbEntity.getIdClass(), classDetails, sourceModelBuildingContext );

// todo : entity-listeners
// todo : exclude default listeners (?)
// todo : exclude superclass listeners (?)
}

public static void processCompleteEmbeddable(
JaxbEntityMappingsImpl jaxbRoot,
JaxbEmbeddableImpl jaxbEmbeddable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import java.util.UUID;

import org.hibernate.annotations.AttributeAccessor;
import org.hibernate.annotations.CollectionType;
import org.hibernate.annotations.Filter;
import org.hibernate.annotations.FilterJoinTable;
import org.hibernate.annotations.CollectionType;
import org.hibernate.annotations.Formula;
import org.hibernate.annotations.JavaType;
import org.hibernate.annotations.JdbcType;
Expand All @@ -44,6 +44,7 @@
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntity;
import org.hibernate.boot.jaxb.mapping.spi.JaxbGeneratedValueImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbHbmFilterImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbIdClassImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbIdImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbLobImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbNationalizedImpl;
Expand Down Expand Up @@ -82,6 +83,7 @@
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.IdClass;
import jakarta.persistence.Inheritance;
import jakarta.persistence.Lob;
import jakarta.persistence.SequenceGenerator;
Expand Down Expand Up @@ -800,7 +802,7 @@ static void applyFilter(
applyFilter( jaxbFilter, target, Filter.class, sourceModelBuildingContext );
}

static void applyJoinTableFilters(
static void applyJoinTableFilter(
JaxbHbmFilterImpl jaxbFilter,
MutableAnnotationTarget target,
SourceModelBuildingContext sourceModelBuildingContext) {
Expand All @@ -811,7 +813,7 @@ private static <F extends Annotation> void applyFilter(
JaxbHbmFilterImpl jaxbFilter,
MutableAnnotationTarget target,
Class<F> filterAnnotationClass,
SourceModelBuildingContext sourceModelBuildingContext) {
SourceModelBuildingContext buildingContext) {
// Since @Filter and @FilterJoinTable have exactly the same attributes,
// we can use the same method with parametrized annotation class
final MutableAnnotationUsage<F> filterAnn = getOrMakeNamedAnnotation(
Expand All @@ -825,15 +827,38 @@ private static <F extends Annotation> void applyFilter(

final List<JaxbHbmFilterImpl.JaxbAliasesImpl> aliases = jaxbFilter.getAliases();
if ( !CollectionHelper.isEmpty( aliases ) ) {
final List<MutableAnnotationUsage<SqlFragmentAlias>> sqlFragmentAliases = new ArrayList<>( aliases.size() );
for ( JaxbHbmFilterImpl.JaxbAliasesImpl alias : aliases ) {
final MutableAnnotationUsage<SqlFragmentAlias> aliasAnn = new DynamicAnnotationUsage<>( SqlFragmentAlias.class );
aliasAnn.setAttributeValue( "alias", alias.getAlias() );
aliasAnn.setAttributeValue( "table", alias.getTable() );
aliasAnn.setAttributeValue( "entity", alias.getEntity() );
sqlFragmentAliases.add( aliasAnn );
filterAnn.setAttributeValue( "aliases", getSqlFragmentAliases( aliases, buildingContext ) );
}
}

private static List<AnnotationUsage<SqlFragmentAlias>> getSqlFragmentAliases(
List<JaxbHbmFilterImpl.JaxbAliasesImpl> aliases,
SourceModelBuildingContext buildingContext) {
final List<AnnotationUsage<SqlFragmentAlias>> sqlFragmentAliases = new ArrayList<>( aliases.size() );
for ( JaxbHbmFilterImpl.JaxbAliasesImpl alias : aliases ) {
final MutableAnnotationUsage<SqlFragmentAlias> aliasAnn = new DynamicAnnotationUsage<>( SqlFragmentAlias.class );
aliasAnn.setAttributeValue( "alias", alias.getAlias() );
applyAttributeIfSpecified( aliasAnn, "table", alias.getTable() );
if ( StringHelper.isNotEmpty( alias.getEntity() ) ) {
aliasAnn.setAttributeValue(
"entity",
buildingContext.getClassDetailsRegistry().resolveClassDetails( alias.getEntity() )
);
}
filterAnn.setAttributeValue( "aliases", sqlFragmentAliases );
sqlFragmentAliases.add( aliasAnn );
}
return sqlFragmentAliases;
}

static void applyIdClass(
JaxbIdClassImpl jaxbIdClass,
MutableClassDetails target,
SourceModelBuildingContext buildingContext) {
if ( jaxbIdClass != null ) {
XmlProcessingHelper.makeAnnotation( IdClass.class, target ).setAttributeValue(
"value",
buildingContext.getClassDetailsRegistry().resolveClassDetails( jaxbIdClass.getClazz() )
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.hibernate.models.source.SourceModelTestHelper;
import org.hibernate.models.source.internal.SourceModelBuildingContextImpl;
import org.hibernate.models.source.spi.AnnotationUsage;
import org.hibernate.models.source.spi.ClassDetails;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -103,5 +104,8 @@ private void validateFilterUsage(AnnotationUsage<Filter> filter) {
assertThat( aliases ).hasSize( 1 );
assertThat( aliases.get( 0 ).<String>getAttributeValue( "alias" ) ).isEqualTo( "t" );
assertThat( aliases.get( 0 ).<String>getAttributeValue( "table" ) ).isEqualTo( "SimpleEntity" );
assertThat( aliases.get( 0 )
.<ClassDetails>getAttributeValue( "entity" )
.getName() ).isEqualTo( SimpleEntity.class.getName() );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@
import org.hibernate.models.orm.spi.ManagedResources;
import org.hibernate.models.orm.spi.ProcessResult;
import org.hibernate.models.orm.spi.Processor;
import org.hibernate.models.orm.xml.SimpleEntity;
import org.hibernate.models.source.SourceModelTestHelper;
import org.hibernate.models.source.internal.SourceModelBuildingContextImpl;
import org.hibernate.models.source.spi.AnnotationUsage;
import org.hibernate.models.source.spi.ClassDetails;
import org.hibernate.models.source.spi.FieldDetails;

import org.junit.jupiter.api.Test;

import org.jboss.jandex.Index;

import jakarta.persistence.IdClass;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hibernate.models.internal.SimpleClassLoading.SIMPLE_CLASS_LOADING;

Expand Down Expand Up @@ -126,4 +129,45 @@ public boolean shouldIgnoreUnlistedClasses() {
assertThat( labelsField.getType().getClassName() ).isEqualTo( Set.class.getName() );
}

@Test
void testIdClass() {
final ManagedResources managedResources = new ManagedResourcesImpl.Builder()
.addXmlMappings( "mappings/dynamic/dynamic-id-class.xml" )
.build();
final Index jandexIndex = SourceModelTestHelper.buildJandexIndex(
SIMPLE_CLASS_LOADING,
Employee.class,
EmployeePK.class
);
final SourceModelBuildingContextImpl buildingContext = SourceModelTestHelper.createBuildingContext(
jandexIndex,
SIMPLE_CLASS_LOADING
);

final ProcessResult processResult = Processor.process(
managedResources,
null,
new Processor.Options() {
@Override
public boolean areGeneratorsGlobal() {
return false;
}

@Override
public boolean shouldIgnoreUnlistedClasses() {
return false;
}
},
buildingContext
);

assertThat( processResult.getEntityHierarchies() ).hasSize( 1 );
final EntityHierarchy hierarchy = processResult.getEntityHierarchies().iterator().next();
final EntityTypeMetadata rootEntity = hierarchy.getRoot();
assertThat( rootEntity.getClassDetails().getName() ).isEqualTo( Employee.class.getName() );

final AnnotationUsage<IdClass> idClass = rootEntity.getClassDetails().getAnnotationUsage( IdClass.class );
assertThat( idClass ).isNotNull();
assertThat( idClass.<ClassDetails>getAttributeValue( "value" ).getName() ).isEqualTo( EmployeePK.class.getName() );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* SPDX-License-Identifier: Apache-2.0
* Copyright: Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.models.orm.xml.dynamic;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;

@Entity
public class Employee {
@Id
private String name;
@Id
private int number;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* SPDX-License-Identifier: Apache-2.0
* Copyright: Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.models.orm.xml.dynamic;

import jakarta.persistence.Id;

public class EmployeePK {
private String name;
private int number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
version="3.1">
<entity class="org.hibernate.models.orm.xml.SimpleEntity" metadata-complete="true" access="FIELD">
<filter name="name_filter" condition="{t}.name = :name">
<aliases alias="t" table="SimpleEntity" />
<aliases alias="t" table="SimpleEntity" entity="org.hibernate.models.orm.xml.SimpleEntity"/>
</filter>

<attributes>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ SPDX-License-Identifier: Apache-2.0
~ Copyright: Red Hat Inc. and Hibernate Authors
-->
<entity-mappings xmlns="http://www.hibernate.org/xsd/orm/mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="3.1">
<entity class="org.hibernate.models.orm.xml.dynamic.Employee" access="FIELD">
<id-class class="org.hibernate.models.orm.xml.dynamic.EmployeePK"/>
<attributes>
<id name="name"/>
<id name="number"/>
</attributes>
</entity>
</entity-mappings>

0 comments on commit d4de89b

Please sign in to comment.