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#26 - Create AnnotationUsage for tenant-id in XML
- Loading branch information
Showing
3 changed files
with
164 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
89 changes: 89 additions & 0 deletions
89
src/test/java/org/hibernate/models/orm/xml/dynamic/TenantIdTest.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,89 @@ | ||
/* | ||
* 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 java.util.Set; | ||
|
||
|
||
import org.hibernate.annotations.TenantId; | ||
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.hibernate.models.spi.FieldDetails; | ||
|
||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
|
||
import jakarta.persistence.Basic; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.FetchType; | ||
|
||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.hibernate.boot.models.categorize.spi.ManagedResourcesProcessor.processManagedResources; | ||
|
||
|
||
public class TenantIdTest { | ||
@Test | ||
void testSimpleDynamicModel() { | ||
final ManagedResources managedResources = new ManagedResourcesImpl.Builder() | ||
.addXmlMappings( "mappings/dynamic/dynamic-tenantid.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( 2 ); | ||
|
||
entityHierarchies.forEach( | ||
entityHierarchy -> { | ||
final EntityTypeMetadata root = entityHierarchy.getRoot(); | ||
final String entityName = root.getEntityName(); | ||
|
||
final FieldDetails field = root.getClassDetails().findFieldByName( "tenantId" ); | ||
|
||
if ( entityName.equals( "EntityWithoutTenantId" ) ) { | ||
assertThat( field ).isNull(); | ||
} | ||
else { | ||
final AnnotationUsage<TenantId> tenantIdAnnotationUsage = field.getAnnotationUsage( TenantId.class ); | ||
|
||
assertThat( tenantIdAnnotationUsage ).isNotNull(); | ||
|
||
final AnnotationUsage<Basic> basicAnnotationUsage = field.getAnnotationUsage( Basic.class ); | ||
assertThat( basicAnnotationUsage ).isNotNull(); | ||
assertThat( basicAnnotationUsage.<FetchType>getAttributeValue( "fetch" ) ) | ||
.isEqualTo( FetchType.EAGER ); | ||
assertThat( basicAnnotationUsage.getBoolean( "optional" ) ).isTrue(); | ||
|
||
final AnnotationUsage<Column> columnAnnotationUsage = field.getAnnotationUsage( Column.class ); | ||
assertThat( basicAnnotationUsage ).isNotNull(); | ||
assertThat( columnAnnotationUsage.getString( "name" ) ).isEqualTo( "TENANT_ID" ); | ||
assertThat( columnAnnotationUsage.getBoolean( "insertable" ) ).isFalse(); | ||
} | ||
} | ||
); | ||
} | ||
} | ||
} |
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,28 @@ | ||
<!-- | ||
~ 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.1"> | ||
<entity name="EntityWithoutTenantId" metadata-complete="true"> | ||
<attributes> | ||
<id name="id"/> | ||
</attributes> | ||
</entity> | ||
|
||
|
||
<entity name="EntityWithTenantId" metadata-complete="true"> | ||
|
||
<tenant-id name="tenantId" fetch="EAGER" optional="true"> | ||
<column name="TENANT_ID" insertable="false"/> | ||
</tenant-id> | ||
|
||
<attributes> | ||
<id name="id"/> | ||
</attributes> | ||
</entity> | ||
|
||
|
||
</entity-mappings> |