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
316 additions
and
0 deletions.
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
129 changes: 129 additions & 0 deletions
129
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,129 @@ | ||
package org.hibernate.models.orm.xml.dynamic; | ||
|
||
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.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 ); | ||
|
||
// check NamedEntityGraph attributeNodes | ||
|
||
AnnotationUsage<NamedAttributeNode> firstAttributeNode = namedAttributeNodeUsage.get( 0 ); | ||
checkAttributeNode( firstAttributeNode, "name", "", "" ); | ||
|
||
AnnotationUsage<NamedAttributeNode> secondAttributeNode = namedAttributeNodeUsage.get( 1 ); | ||
checkAttributeNode( secondAttributeNode, "address", "employee.address", "" ); | ||
|
||
// check NamedEntityGraph subgraphs | ||
final List<AnnotationUsage<NamedSubgraph>> subgraphUsages = namedEntityGraphAnnotationUsage | ||
.getAttributeValue( "subgraphs" ); | ||
assertThat( subgraphUsages ).size().isEqualTo( 2 ); | ||
|
||
AnnotationUsage<NamedSubgraph> firstSubgraph = subgraphUsages.get( 0 ); | ||
assertThat( firstSubgraph.getString( "name" ) ).isEqualTo( "first.subgraph" ); | ||
assertThat( firstSubgraph.<ClassDetails>getAttributeValue( "type" ).getName() ) | ||
.isEqualTo( void.class.getName() ); | ||
|
||
// check first NamedSubgraph attributeNodes | ||
|
||
namedAttributeNodeUsage = firstSubgraph.getAttributeValue( "attributeNodes" ); | ||
assertThat( namedAttributeNodeUsage ).size().isEqualTo( 1 ); | ||
|
||
checkAttributeNode( namedAttributeNodeUsage.get( 0 ), "city", "", "" ); | ||
|
||
AnnotationUsage<NamedSubgraph> secondSubgraph = subgraphUsages.get( 1 ); | ||
assertThat( secondSubgraph.getString( "name" ) ).isEqualTo( "second.subgraph" ); | ||
assertThat( secondSubgraph.<ClassDetails>getAttributeValue( "type" ).getName() ) | ||
.isEqualTo( String.class.getName() ); | ||
|
||
namedAttributeNodeUsage = secondSubgraph.getAttributeValue( "attributeNodes" ); | ||
assertThat( namedAttributeNodeUsage ).size().isEqualTo( 3 ); | ||
|
||
// check second NamedSubgraph attributeNodes | ||
checkAttributeNode( namedAttributeNodeUsage.get( 0 ), "city", "sub1", "" ); | ||
checkAttributeNode( namedAttributeNodeUsage.get( 1 ), "name", "sub", "" ); | ||
checkAttributeNode( namedAttributeNodeUsage.get( 2 ), "surname", "", "" ); | ||
|
||
|
||
final List<AnnotationUsage<NamedSubgraph>> subClassSubgraphUsages = namedEntityGraphAnnotationUsage | ||
.getAttributeValue( "subclassSubgraphs" ); | ||
assertThat( subClassSubgraphUsages ).size().isEqualTo( 0 ); | ||
|
||
} | ||
} | ||
); | ||
} | ||
} | ||
|
||
private static void checkAttributeNode( | ||
AnnotationUsage<NamedAttributeNode> firstAttributeNode, | ||
String expectedValueName, | ||
String expectedSubgraph, | ||
String expectedKeySubgraph) { | ||
assertThat( firstAttributeNode.getString( "value" ) ).isEqualTo( expectedValueName ); | ||
assertThat( firstAttributeNode.getString( "subgraph" ) ).isEqualTo( expectedSubgraph ); | ||
assertThat( firstAttributeNode.getString( "keySubgraph" ) ).isEqualTo( expectedKeySubgraph ); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
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,36 @@ | ||
<!-- | ||
~ 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="first.subgraph"> | ||
<named-attribute-node name="city"/> | ||
</subgraph> | ||
<subgraph name="second.subgraph" class="String"> | ||
<named-attribute-node name="city" subgraph="sub1"/> | ||
<named-attribute-node name="name" subgraph="sub"/> | ||
<named-attribute-node name="surname" /> | ||
</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> |