forked from sebersole/hibernate-models2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sebersole#25 - Create AnnotationUsage for named-entity-graph in XML
- Loading branch information
Showing
4 changed files
with
325 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
src/test/java/org/hibernate/models/orm/xml/dynamic/NamedEntityGraphTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package org.hibernate.models.orm.xml.dynamic; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import org.hibernate.boot.internal.BootstrapContextImpl; | ||
import org.hibernate.boot.internal.MetadataBuilderImpl; | ||
import org.hibernate.boot.model.process.spi.ManagedResources; | ||
import org.hibernate.boot.models.categorize.spi.CategorizedDomainModel; | ||
import org.hibernate.boot.models.categorize.spi.EntityHierarchy; | ||
import org.hibernate.boot.models.categorize.spi.EntityTypeMetadata; | ||
import org.hibernate.boot.registry.StandardServiceRegistry; | ||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; | ||
import org.hibernate.models.orm.process.ManagedResourcesImpl; | ||
import org.hibernate.models.spi.AnnotationUsage; | ||
import org.hibernate.models.spi.ClassDetails; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import jakarta.persistence.NamedAttributeNode; | ||
import jakarta.persistence.NamedEntityGraph; | ||
import jakarta.persistence.NamedSubgraph; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.fail; | ||
import static org.hibernate.boot.models.categorize.spi.ManagedResourcesProcessor.processManagedResources; | ||
|
||
public class NamedEntityGraphTest { | ||
@Test | ||
void testNamedEntityGraph() { | ||
final ManagedResources managedResources = new ManagedResourcesImpl.Builder() | ||
.addXmlMappings( "mappings/dynamic/dynamic-named-entity-graph.xml" ) | ||
.build(); | ||
try (StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().build()) { | ||
final BootstrapContextImpl bootstrapContext = new BootstrapContextImpl( | ||
serviceRegistry, | ||
new MetadataBuilderImpl.MetadataBuildingOptionsImpl( serviceRegistry ) | ||
); | ||
final CategorizedDomainModel categorizedDomainModel = processManagedResources( | ||
managedResources, | ||
bootstrapContext | ||
); | ||
|
||
final Set<EntityHierarchy> entityHierarchies = categorizedDomainModel.getEntityHierarchies(); | ||
assertThat( entityHierarchies ).hasSize( 1 ); | ||
|
||
entityHierarchies.forEach( | ||
entityHierarchy -> { | ||
final EntityTypeMetadata root = entityHierarchy.getRoot(); | ||
final String entityName = root.getEntityName(); | ||
|
||
final AnnotationUsage<NamedEntityGraph> namedEntityGraphAnnotationUsage = root.getClassDetails() | ||
.getAnnotationUsage( NamedEntityGraph.class ); | ||
|
||
if ( entityName.equals( "Address" ) ) { | ||
assertThat( namedEntityGraphAnnotationUsage ).isNull(); | ||
} | ||
else { | ||
assertThat( namedEntityGraphAnnotationUsage ).isNotNull(); | ||
|
||
final String graphName = namedEntityGraphAnnotationUsage.getAttributeValue( "name" ); | ||
assertThat( graphName ).isEqualTo( "employee" ); | ||
|
||
final boolean includeAllAttributes = namedEntityGraphAnnotationUsage.getAttributeValue( | ||
"includeAllAttributes" ); | ||
assertThat( includeAllAttributes ).isTrue(); | ||
|
||
List<AnnotationUsage<NamedAttributeNode>> namedAttributeNodeUsage = namedEntityGraphAnnotationUsage | ||
.getAttributeValue( "attributeNodes" ); | ||
assertThat( namedAttributeNodeUsage ).size().isEqualTo( 2 ); | ||
|
||
final Set<String> namedAttributeNodeValues = new HashSet<>(); | ||
|
||
for ( AnnotationUsage<NamedAttributeNode> attributeNode : namedAttributeNodeUsage ) { | ||
final String attributeNodeValue = attributeNode.getString( "value" ); | ||
final String subgraph = attributeNode.getString( "subgraph" ); | ||
final String keySubgraph = attributeNode.getString( "keySubgraph" ); | ||
namedAttributeNodeValues.add( attributeNodeValue ); | ||
if ( attributeNodeValue.equals( "name" ) ) { | ||
assertThat( subgraph ).isEqualTo( "" ); | ||
assertThat( keySubgraph ).isEqualTo( "" ); | ||
} | ||
else if ( attributeNodeValue.equals( "address" ) ) { | ||
assertThat( subgraph ).isEqualTo( "employee.address" ); | ||
assertThat( keySubgraph ).isEqualTo( "" ); | ||
} | ||
else { | ||
fail( "NamedEntityGraph, wrong NamedAttributeNode value : " + attributeNodeValue ); | ||
} | ||
} | ||
assertThat( namedAttributeNodeValues ).size().isEqualTo( 2 ); | ||
assertThat( namedAttributeNodeValues ).contains( "name" ); | ||
assertThat( namedAttributeNodeValues ).contains( "address" ); | ||
|
||
final List<AnnotationUsage<NamedSubgraph>> subgraphUsages = namedEntityGraphAnnotationUsage | ||
.getAttributeValue( "subgraphs" ); | ||
assertThat( subgraphUsages ).size().isEqualTo( 2 ); | ||
|
||
AnnotationUsage<NamedSubgraph> subgraphUsage = subgraphUsages.get( 0 ); | ||
assertThat( subgraphUsage.getString( "name" )).isEqualTo( "employee.address.city" ); | ||
assertThat( subgraphUsage.<ClassDetails>getAttributeValue( "type" ).getName() ).isEqualTo( void.class.getName() ); | ||
|
||
namedAttributeNodeUsage = subgraphUsage.getAttributeValue( "attributeNodes" ); | ||
assertThat( namedAttributeNodeUsage ).size().isEqualTo( 1 ); | ||
|
||
AnnotationUsage<NamedAttributeNode> attributeNode = namedAttributeNodeUsage.get( 0 ); | ||
assertThat( attributeNode.getString( "value" ) ).isEqualTo( "city" ); | ||
assertThat( attributeNode.getString( "subgraph" ) ).isEqualTo( "" ); | ||
assertThat( attributeNode.getString( "keySubgraph" ) ).isEqualTo( "" ); | ||
|
||
subgraphUsage = subgraphUsages.get( 1 ); | ||
assertThat( subgraphUsage.getString( "name" )).isEqualTo( "employee.address.cityAndName" ); | ||
assertThat( subgraphUsage.<ClassDetails>getAttributeValue( "type" ).getName() ).isEqualTo( String.class.getName() ); | ||
|
||
namedAttributeNodeUsage = subgraphUsage.getAttributeValue( "attributeNodes" ); | ||
assertThat( namedAttributeNodeUsage ).size().isEqualTo( 2 ); | ||
|
||
attributeNode = namedAttributeNodeUsage.get( 0 ); | ||
assertThat( attributeNode.getString( "value" ) ).isEqualTo( "city" ); | ||
assertThat( attributeNode.getString( "subgraph" ) ).isEqualTo( "sub1" ); | ||
assertThat( attributeNode.getString( "keySubgraph" ) ).isEqualTo( "" ); | ||
|
||
attributeNode = namedAttributeNodeUsage.get( 1 ); | ||
assertThat( attributeNode.getString( "value" ) ).isEqualTo( "name" ); | ||
assertThat( attributeNode.getString( "subgraph" ) ).isEqualTo( "sub" ); | ||
assertThat( attributeNode.getString( "keySubgraph" ) ).isEqualTo( "" ); | ||
|
||
final List<AnnotationUsage<NamedSubgraph>> subClassSubgraphUsages = namedEntityGraphAnnotationUsage | ||
.getAttributeValue( "subclassSubgraphs" ); | ||
assertThat( subClassSubgraphUsages ).size().isEqualTo( 0 ); | ||
|
||
} | ||
} | ||
); | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/test/resources/mappings/dynamic/dynamic-named-entity-graph.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<!-- | ||
~ 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" | ||
version="3.2"> | ||
<entity name="Employee" metadata-complete="true"> | ||
<named-entity-graph name="employee" include-all-attributes="true"> | ||
<named-attribute-node name="name"/> | ||
<named-attribute-node name="address" subgraph="employee.address"/> | ||
<subgraph name="employee.address.city"> | ||
<named-attribute-node name="city"/> | ||
</subgraph> | ||
<subgraph name="employee.address.cityAndName" class="String"> | ||
<named-attribute-node name="city" subgraph="sub1"/> | ||
<named-attribute-node name="name" subgraph="sub"/> | ||
</subgraph> | ||
</named-entity-graph> | ||
<attributes> | ||
<id name="id"/> | ||
<basic name="name"/> | ||
<basic name="surname"/> | ||
<!-- <one-to-one name="address" fetch="LAZY"/>--> | ||
</attributes> | ||
</entity> | ||
<!-- <entity name="Address" metadata-complete="true">--> | ||
<!-- <attributes>--> | ||
<!-- <id name="id"/>--> | ||
<!-- <basic name="city"/>--> | ||
|
||
<!-- </attributes>--> | ||
<!-- </entity>--> | ||
</entity-mappings> |