Skip to content

Commit

Permalink
sebersole#61 - Collect natural-ids as part of EntityHierarchy building
Browse files Browse the repository at this point in the history
  • Loading branch information
sebersole committed Nov 2, 2023
1 parent f290595 commit 570cb49
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
*/
package org.hibernate.models.orm.bind;

import org.hibernate.annotations.NaturalId;
import org.hibernate.annotations.TenantId;

import jakarta.persistence.Embeddable;
import jakarta.persistence.Embedded;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
Expand All @@ -22,6 +24,9 @@
public class AggregatedIdEntity {
@EmbeddedId
private Pk id;
@Embedded
@NaturalId
private NatKey naturalId;

@Version
private Integer version;
Expand All @@ -33,4 +38,10 @@ public static class Pk {
private Integer id1;
private Integer id2;
}

@Embeddable
public static class NatKey {
private Integer key1;
private Integer key2;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Set;
import java.util.stream.Collectors;

import org.hibernate.annotations.NaturalId;
import org.hibernate.annotations.TenantId;
import org.hibernate.boot.internal.BootstrapContextImpl;
import org.hibernate.boot.internal.MetadataBuilderImpl;
Expand Down Expand Up @@ -52,6 +53,12 @@ void testSimpleId() {
assertThat( idMapping.getAttribute().getMember().getAnnotationUsage( Id.class ) ).isNotNull();
assertThat( idMapping.getAttribute().getMember().getAnnotationUsage( EmbeddedId.class ) ).isNull();

assertThat( entityHierarchy.getNaturalIdMapping() ).isNotNull();
final BasicKeyMapping naturalIdMapping = (BasicKeyMapping) entityHierarchy.getNaturalIdMapping();
assertThat( naturalIdMapping.getAttribute().getMember().getAnnotationUsage( NaturalId.class ) ).isNotNull();
assertThat( naturalIdMapping.getAttribute().getMember().getAnnotationUsage( Id.class ) ).isNull();
assertThat( naturalIdMapping.getAttribute().getMember().getAnnotationUsage( EmbeddedId.class ) ).isNull();

assertThat( entityHierarchy.getVersionAttribute() ).isNotNull();
assertThat( entityHierarchy.getVersionAttribute().getMember().getAnnotationUsage( Version.class ) ).isNotNull();

Expand All @@ -70,6 +77,13 @@ void testAggregatedId() {
assertThat( idMapping.getAttribute().getMember().getAnnotationUsage( Id.class ) ).isNull();
assertThat( idMapping.getAttribute().getMember().getAnnotationUsage( EmbeddedId.class ) ).isNotNull();

assertThat( entityHierarchy.getNaturalIdMapping() ).isNotNull();
assertThat( entityHierarchy.getNaturalIdMapping() ).isInstanceOf( AggregatedKeyMapping.class );
final AggregatedKeyMapping naturalIdMapping = (AggregatedKeyMapping) entityHierarchy.getNaturalIdMapping();
assertThat( naturalIdMapping.getAttribute().getMember().getAnnotationUsage( Id.class ) ).isNull();
assertThat( naturalIdMapping.getAttribute().getMember().getAnnotationUsage( EmbeddedId.class ) ).isNull();
assertThat( naturalIdMapping.getAttribute().getMember().getAnnotationUsage( NaturalId.class ) ).isNotNull();

assertThat( entityHierarchy.getVersionAttribute() ).isNotNull();
assertThat( entityHierarchy.getVersionAttribute().getMember().getAnnotationUsage( Version.class ) ).isNotNull();

Expand All @@ -78,15 +92,21 @@ void testAggregatedId() {
}

@Test
void testHierarchy() {
final Set<EntityHierarchy> entityHierarchies = buildHierarchyMetadata( HierarchyRoot.class, HierarchySuper.class );
void testNonAggregatedId() {
final Set<EntityHierarchy> entityHierarchies = buildHierarchyMetadata( NonAggregatedIdEntity.class );
assertThat( entityHierarchies ).hasSize( 1 );
final EntityHierarchy entityHierarchy = entityHierarchies.iterator().next();

assertThat( entityHierarchy.getIdMapping() ).isNotNull();
final BasicKeyMapping idMapping = (BasicKeyMapping) entityHierarchy.getIdMapping();
assertThat( idMapping.getAttribute().getMember().getAnnotationUsage( Id.class ) ).isNotNull();
assertThat( idMapping.getAttribute().getMember().getAnnotationUsage( EmbeddedId.class ) ).isNull();
final NonAggregatedKeyMapping idMapping = (NonAggregatedKeyMapping) entityHierarchy.getIdMapping();
assertThat( idMapping.getIdAttributes() ).hasSize( 2 );
assertThat( idMapping.getIdAttributes().stream().map( AttributeMetadata::getName ) ).containsExactly( "id1", "id2" );
assertThat( idMapping.getIdClassType().getClassName() ).isEqualTo( NonAggregatedIdEntity.Pk.class.getName() );

assertThat( entityHierarchy.getNaturalIdMapping() ).isNotNull();
final NonAggregatedKeyMapping naturalIdMapping = (NonAggregatedKeyMapping) entityHierarchy.getNaturalIdMapping();
assertThat( naturalIdMapping.getIdAttributes() ).hasSize( 2 );
assertThat( naturalIdMapping.getIdAttributes().stream().map( AttributeMetadata::getName ) ).containsExactly( "naturalKey1", "naturalKey2" );

assertThat( entityHierarchy.getVersionAttribute() ).isNotNull();
assertThat( entityHierarchy.getVersionAttribute().getMember().getAnnotationUsage( Version.class ) ).isNotNull();
Expand All @@ -95,23 +115,22 @@ void testHierarchy() {
assertThat( entityHierarchy.getTenantIdAttribute().getMember().getAnnotationUsage( TenantId.class ) ).isNotNull();

assertThat( entityHierarchy.getCacheRegion() ).isNotNull();
assertThat( entityHierarchy.getCacheRegion().getAccessType() ).isEqualTo( AccessType.READ_ONLY );
assertThat( entityHierarchy.getCacheRegion().getAccessType() ).isEqualTo( AccessType.TRANSACTIONAL );

assertThat( entityHierarchy.getInheritanceType() ).isNotNull();
assertThat( entityHierarchy.getInheritanceType() ).isEqualTo( InheritanceType.JOINED );
assertThat( entityHierarchy.getInheritanceType() ).isEqualTo( InheritanceType.TABLE_PER_CLASS );
}

@Test
void testNonAggregatedId() {
final Set<EntityHierarchy> entityHierarchies = buildHierarchyMetadata( NonAggregatedIdEntity.class );
void testHierarchy() {
final Set<EntityHierarchy> entityHierarchies = buildHierarchyMetadata( HierarchyRoot.class, HierarchySuper.class );
assertThat( entityHierarchies ).hasSize( 1 );
final EntityHierarchy entityHierarchy = entityHierarchies.iterator().next();

assertThat( entityHierarchy.getIdMapping() ).isNotNull();
final NonAggregatedKeyMapping idMapping = (NonAggregatedKeyMapping) entityHierarchy.getIdMapping();
assertThat( idMapping.getIdAttributes() ).hasSize( 2 );
assertThat( idMapping.getIdAttributes().stream().map( AttributeMetadata::getName ) ).containsExactly( "id1", "id2" );
assertThat( idMapping.getIdClassType().getClassName() ).isEqualTo( NonAggregatedIdEntity.Pk.class.getName() );
final BasicKeyMapping idMapping = (BasicKeyMapping) entityHierarchy.getIdMapping();
assertThat( idMapping.getAttribute().getMember().getAnnotationUsage( Id.class ) ).isNotNull();
assertThat( idMapping.getAttribute().getMember().getAnnotationUsage( EmbeddedId.class ) ).isNull();

assertThat( entityHierarchy.getVersionAttribute() ).isNotNull();
assertThat( entityHierarchy.getVersionAttribute().getMember().getAnnotationUsage( Version.class ) ).isNotNull();
Expand All @@ -120,10 +139,10 @@ void testNonAggregatedId() {
assertThat( entityHierarchy.getTenantIdAttribute().getMember().getAnnotationUsage( TenantId.class ) ).isNotNull();

assertThat( entityHierarchy.getCacheRegion() ).isNotNull();
assertThat( entityHierarchy.getCacheRegion().getAccessType() ).isEqualTo( AccessType.TRANSACTIONAL );
assertThat( entityHierarchy.getCacheRegion().getAccessType() ).isEqualTo( AccessType.READ_ONLY );

assertThat( entityHierarchy.getInheritanceType() ).isNotNull();
assertThat( entityHierarchy.getInheritanceType() ).isEqualTo( InheritanceType.TABLE_PER_CLASS );
assertThat( entityHierarchy.getInheritanceType() ).isEqualTo( InheritanceType.JOINED );
}

private static Set<EntityHierarchy> buildHierarchyMetadata(Class<?>... classes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.NaturalId;
import org.hibernate.annotations.RowId;
import org.hibernate.annotations.TenantId;

Expand All @@ -32,6 +33,8 @@ public class NonAggregatedIdEntity {
@Id private Integer id2;
@Version private Integer version;
@TenantId private String tenantId;
@NaturalId private Integer naturalKey1;
@NaturalId private Integer naturalKey2;

public static class Pk {
private Integer id1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package org.hibernate.models.orm.bind;

import org.hibernate.annotations.NaturalId;
import org.hibernate.annotations.TenantId;

import jakarta.persistence.Entity;
Expand All @@ -23,4 +24,7 @@ public class SimpleIdEntity {
private Integer version;
@TenantId
private String tenantId;
@NaturalId
private String naturalId;

}

0 comments on commit 570cb49

Please sign in to comment.