From ea4e26566fd6b2ae1575414587f12e2107612ef8 Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Tue, 24 Oct 2023 14:01:49 +0200 Subject: [PATCH] #27 - --wip-- final `@Filter` work and tests --- .../orm/internal/GlobalRegistrationsImpl.java | 2 +- .../orm/xml/internal/XmlAnnotationHelper.java | 18 +++++++----------- .../xml/complete/SimpleCompleteXmlTests.java | 16 ++++++++++++++++ .../mappings/complete/simple-complete.xml | 4 ++++ .../hibernate/xsd/mapping/mapping-3.1.0.xsd | 12 ++++++++++-- .../src/main/xjb/mapping-bindings.xjb | 6 +++++- 6 files changed, 43 insertions(+), 15 deletions(-) diff --git a/hibernate-models-orm/src/main/java/org/hibernate/models/orm/internal/GlobalRegistrationsImpl.java b/hibernate-models-orm/src/main/java/org/hibernate/models/orm/internal/GlobalRegistrationsImpl.java index 3d0f37e..940042a 100644 --- a/hibernate-models-orm/src/main/java/org/hibernate/models/orm/internal/GlobalRegistrationsImpl.java +++ b/hibernate-models-orm/src/main/java/org/hibernate/models/orm/internal/GlobalRegistrationsImpl.java @@ -443,7 +443,7 @@ public void collectFilterDefinitions(List filterDefinitions) } private Map extractFilterParameters(JaxbFilterDefImpl source) { - final List parameters = source.getFilterParam(); + final List parameters = source.getFilterParams(); if ( isEmpty( parameters ) ) { return null; } diff --git a/hibernate-models-orm/src/main/java/org/hibernate/models/orm/xml/internal/XmlAnnotationHelper.java b/hibernate-models-orm/src/main/java/org/hibernate/models/orm/xml/internal/XmlAnnotationHelper.java index 1dc5948..823a37b 100644 --- a/hibernate-models-orm/src/main/java/org/hibernate/models/orm/xml/internal/XmlAnnotationHelper.java +++ b/hibernate-models-orm/src/main/java/org/hibernate/models/orm/xml/internal/XmlAnnotationHelper.java @@ -820,21 +820,17 @@ private static void applyFilter( applyAttributeIfSpecified( filterAnn, "condition", jaxbFilter.getCondition() ); applyAttributeIfSpecified( filterAnn, "deduceAliasInjectionPoints", jaxbFilter.isAutoAliasInjection() ); - final List content = jaxbFilter.getContent(); - if ( !isEmpty( content ) ) { - final List> aliases = arrayList( content.size() ); - for ( Serializable c : content ) { - final JaxbHbmFilterImpl.JaxbAliasesImpl alias = (JaxbHbmFilterImpl.JaxbAliasesImpl) c; - final MutableAnnotationUsage aliasAnn = makeAnnotation( - SqlFragmentAlias.class, - null - ); + final List aliases = jaxbFilter.getAliases(); + if ( !isEmpty( aliases ) ) { + final List> sqlFragmentAliases = arrayList( aliases.size() ); + for ( JaxbHbmFilterImpl.JaxbAliasesImpl alias : aliases ) { + final MutableAnnotationUsage aliasAnn = new DynamicAnnotationUsage<>( SqlFragmentAlias.class ); aliasAnn.setAttributeValue( "alias", alias.getAlias() ); aliasAnn.setAttributeValue( "table", alias.getTable() ); aliasAnn.setAttributeValue( "entity", alias.getEntity() ); - aliases.add( aliasAnn ); + sqlFragmentAliases.add( aliasAnn ); } - filterAnn.setAttributeValue( "aliases", aliases ); + filterAnn.setAttributeValue( "aliases", sqlFragmentAliases ); } } } diff --git a/hibernate-models-orm/src/test/java/org/hibernate/models/orm/xml/complete/SimpleCompleteXmlTests.java b/hibernate-models-orm/src/test/java/org/hibernate/models/orm/xml/complete/SimpleCompleteXmlTests.java index 81ba866..98c2300 100644 --- a/hibernate-models-orm/src/test/java/org/hibernate/models/orm/xml/complete/SimpleCompleteXmlTests.java +++ b/hibernate-models-orm/src/test/java/org/hibernate/models/orm/xml/complete/SimpleCompleteXmlTests.java @@ -6,6 +6,10 @@ */ package org.hibernate.models.orm.xml.complete; +import java.util.List; + +import org.hibernate.annotations.Filter; +import org.hibernate.annotations.SqlFragmentAlias; import org.hibernate.models.orm.internal.ManagedResourcesImpl; import org.hibernate.models.orm.spi.AttributeMetadata; import org.hibernate.models.orm.spi.EntityHierarchy; @@ -87,5 +91,17 @@ public boolean shouldIgnoreUnlistedClasses() { final AnnotationUsage nameColumnAnn = nameAttribute.getMember().getAnnotationUsage( Column.class ); assertThat( nameColumnAnn ).isNotNull(); assertThat( nameColumnAnn.getAttributeValue( "name" ) ).isEqualTo( "description" ); + + validateFilterUsage( root.getClassDetails().getAnnotationUsage( Filter.class ) ); + } + + private void validateFilterUsage(AnnotationUsage filter) { + assertThat( filter ).isNotNull(); + assertThat( filter.getAttributeValue( "name" ) ).isEqualTo( "name_filter" ); + assertThat( filter.getAttributeValue( "condition" ) ).isEqualTo( "{t}.name = :name" ); + final List> aliases = filter.getAttributeValue( "aliases" ); + assertThat( aliases ).hasSize( 1 ); + assertThat( aliases.get( 0 ).getAttributeValue( "alias" ) ).isEqualTo( "t" ); + assertThat( aliases.get( 0 ).getAttributeValue( "table" ) ).isEqualTo( "SimpleEntity" ); } } diff --git a/hibernate-models-orm/src/test/resources/mappings/complete/simple-complete.xml b/hibernate-models-orm/src/test/resources/mappings/complete/simple-complete.xml index f030a2d..b797fcf 100644 --- a/hibernate-models-orm/src/test/resources/mappings/complete/simple-complete.xml +++ b/hibernate-models-orm/src/test/resources/mappings/complete/simple-complete.xml @@ -9,6 +9,10 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.1"> + + + + diff --git a/hibernate-orm/src/main/resources/org/hibernate/xsd/mapping/mapping-3.1.0.xsd b/hibernate-orm/src/main/resources/org/hibernate/xsd/mapping/mapping-3.1.0.xsd index bcfcdd2..4cdb217 100644 --- a/hibernate-orm/src/main/resources/org/hibernate/xsd/mapping/mapping-3.1.0.xsd +++ b/hibernate-orm/src/main/resources/org/hibernate/xsd/mapping/mapping-3.1.0.xsd @@ -2506,7 +2506,7 @@ - + Applies a filter defined by hbm-filter-def usage @@ -2525,7 +2525,6 @@ - @@ -2533,6 +2532,15 @@ + + + + + + + + + diff --git a/hibernate-orm/src/main/xjb/mapping-bindings.xjb b/hibernate-orm/src/main/xjb/mapping-bindings.xjb index e13eba3..a0b39ba 100644 --- a/hibernate-orm/src/main/xjb/mapping-bindings.xjb +++ b/hibernate-orm/src/main/xjb/mapping-bindings.xjb @@ -457,7 +457,11 @@ - + + + + +