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#22 - Create AnnotationUsage for discriminator-value, discri…
…minator-column and discriminator-formula in XML
- Loading branch information
Showing
4 changed files
with
266 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
124 changes: 124 additions & 0 deletions
124
src/test/java/org/hibernate/models/orm/xml/complete/DiscriminatorValueTest.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,124 @@ | ||
package org.hibernate.models.orm.xml.complete; | ||
|
||
import java.util.Set; | ||
|
||
import org.hibernate.annotations.DiscriminatorFormula; | ||
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.models.categorize.spi.IdentifiableTypeMetadata; | ||
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.junit.jupiter.api.Test; | ||
|
||
import jakarta.persistence.DiscriminatorColumn; | ||
import jakarta.persistence.DiscriminatorType; | ||
import jakarta.persistence.DiscriminatorValue; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.hibernate.boot.models.categorize.spi.ManagedResourcesProcessor.processManagedResources; | ||
|
||
public class DiscriminatorValueTest { | ||
@Test | ||
void testDiscriminatorValue() { | ||
final ManagedResources managedResources = new ManagedResourcesImpl.Builder() | ||
.addXmlMappings( "mappings/complete/discriminator-value.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( 3 ); | ||
|
||
for ( EntityHierarchy entityHierarchy : entityHierarchies ) { | ||
final EntityTypeMetadata root = entityHierarchy.getRoot(); | ||
|
||
final String entityName = root.getClassDetails().getName(); | ||
if ( entityName.equals( "org.hibernate.models.orm.xml.complete.Root" ) ) { | ||
|
||
final AnnotationUsage<DiscriminatorValue> rootDiscriminatorValueAnnotationUsage = root.getClassDetails() | ||
.getAnnotationUsage( DiscriminatorValue.class ); | ||
assertThat( rootDiscriminatorValueAnnotationUsage ).isNull(); | ||
|
||
final AnnotationUsage<DiscriminatorColumn> discriminatorColumnAnnotationUsage = root.getClassDetails() | ||
.getAnnotationUsage( DiscriminatorColumn.class ); | ||
|
||
assertThat( discriminatorColumnAnnotationUsage ).isNotNull(); | ||
|
||
final String discriminatorColumName = discriminatorColumnAnnotationUsage.getString( "name" ); | ||
assertThat( discriminatorColumName ).isEqualTo( "TYPE_COLUMN" ); | ||
|
||
final DiscriminatorType discriminatorColumnType = discriminatorColumnAnnotationUsage | ||
.getEnum( "discriminatorType" ); | ||
assertThat( discriminatorColumnType ).isEqualTo( DiscriminatorType.INTEGER ); | ||
|
||
final Iterable<IdentifiableTypeMetadata> subTypes = root.getSubTypes(); | ||
assertThat( subTypes ).hasSize( 1 ); | ||
|
||
final IdentifiableTypeMetadata subType = subTypes.iterator().next(); | ||
final AnnotationUsage<DiscriminatorValue> subTypeDiscriminatorValueAnnotationUsage = subType.getClassDetails() | ||
.getAnnotationUsage( DiscriminatorValue.class ); | ||
assertThat( subTypeDiscriminatorValueAnnotationUsage ).isNotNull(); | ||
String discriminatorValue = subTypeDiscriminatorValueAnnotationUsage.getString( "value" ); | ||
assertThat( discriminatorValue ).isEqualTo( "R" ); | ||
|
||
final AnnotationUsage<DiscriminatorFormula> discriminatorFortmulaAnnotationUsage = root.getClassDetails() | ||
.getAnnotationUsage( DiscriminatorFormula.class ); | ||
assertThat( discriminatorFortmulaAnnotationUsage ).isNull(); | ||
} | ||
else if ( entityName.equals( "org.hibernate.models.orm.xml.complete.SimplePerson" ) ) { | ||
final AnnotationUsage<DiscriminatorValue> rootDiscriminatorValueAnnotationUsage = root.getClassDetails() | ||
.getAnnotationUsage( DiscriminatorValue.class ); | ||
assertThat( rootDiscriminatorValueAnnotationUsage ).isNull(); | ||
|
||
final AnnotationUsage<DiscriminatorColumn> discriminatorColumnAnnotationUsage = root.getClassDetails() | ||
.getAnnotationUsage( DiscriminatorColumn.class ); | ||
assertThat( discriminatorColumnAnnotationUsage ).isNotNull(); | ||
|
||
final String discriminatorColumName = discriminatorColumnAnnotationUsage.getString( "name" ); | ||
assertThat( discriminatorColumName ).isEqualTo( "DTYPE" ); | ||
|
||
final DiscriminatorType discriminatorColumnType = discriminatorColumnAnnotationUsage | ||
.getEnum( "discriminatorType" ); | ||
assertThat( discriminatorColumnType ).isEqualTo( DiscriminatorType.STRING ); | ||
|
||
final AnnotationUsage<DiscriminatorFormula> discriminatorFortmulaAnnotationUsage = root.getClassDetails() | ||
.getAnnotationUsage( DiscriminatorFormula.class ); | ||
assertThat( discriminatorFortmulaAnnotationUsage ).isNull(); | ||
} | ||
else { | ||
assertThat( entityName ).isEqualTo( "org.hibernate.models.orm.xml.SimpleEntity" ); | ||
|
||
final AnnotationUsage<DiscriminatorValue> rootDiscriminatorValueAnnotationUsage = root.getClassDetails() | ||
.getAnnotationUsage( DiscriminatorValue.class ); | ||
assertThat( rootDiscriminatorValueAnnotationUsage ).isNull(); | ||
|
||
final AnnotationUsage<DiscriminatorColumn> discriminatorColumnAnnotationUsage = root.getClassDetails() | ||
.getAnnotationUsage( DiscriminatorColumn.class ); | ||
assertThat( discriminatorColumnAnnotationUsage ).isNull(); | ||
|
||
final AnnotationUsage<DiscriminatorFormula> discriminatorFortmulaAnnotationUsage = root.getClassDetails() | ||
.getAnnotationUsage( DiscriminatorFormula.class ); | ||
assertThat( discriminatorFortmulaAnnotationUsage ).isNotNull(); | ||
|
||
final String formula = discriminatorFortmulaAnnotationUsage.getString( "value" ); | ||
assertThat( formula ).isEqualTo( "CASE WHEN VALUE1 IS NOT NULL THEN 1 WHEN VALUE2 IS NOT NULL THEN 2 END" ); | ||
} | ||
} | ||
|
||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/test/resources/mappings/complete/discriminator-value.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,43 @@ | ||
<!-- | ||
~ 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.2"> | ||
<package>org.hibernate.models.orm.xml.complete</package> | ||
|
||
<entity class="Root" metadata-complete="true" access="FIELD"> | ||
<inheritance strategy="JOINED"/> | ||
<discriminator-column name="TYPE_COLUMN" discriminator-type="INTEGER"/> | ||
<attributes> | ||
<id name="id"> | ||
<column name="pk"/> | ||
</id> | ||
<basic name="name"/> | ||
</attributes> | ||
</entity> | ||
<entity class="Sub" metadata-complete="true" access="FIELD"> | ||
<extends>Root</extends> | ||
<discriminator-value>R</discriminator-value> | ||
<attributes> | ||
<basic name="subName"/> | ||
</attributes> | ||
</entity> | ||
|
||
<entity class="SimplePerson" metadata-complete="true" access="FIELD"> | ||
<discriminator-column/> | ||
<attributes> | ||
<id name="id"/> | ||
</attributes> | ||
</entity> | ||
|
||
<entity class="org.hibernate.models.orm.xml.SimpleEntity" metadata-complete="true" access="FIELD"> | ||
<discriminator-formula>CASE WHEN VALUE1 IS NOT NULL THEN 1 WHEN VALUE2 IS NOT NULL THEN 2 END</discriminator-formula> | ||
<attributes> | ||
<id name="id"/> | ||
</attributes> | ||
</entity> | ||
</entity-mappings> |