Skip to content

Commit

Permalink
sebersole#27 - --wip-- final @Filter work and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mbladel committed Oct 24, 2023
1 parent 580ca02 commit ea4e265
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ public void collectFilterDefinitions(List<JaxbFilterDefImpl> filterDefinitions)
}

private Map<String, ClassDetails> extractFilterParameters(JaxbFilterDefImpl source) {
final List<JaxbFilterDefImpl.JaxbFilterParamImpl> parameters = source.getFilterParam();
final List<JaxbFilterDefImpl.JaxbFilterParamImpl> parameters = source.getFilterParams();
if ( isEmpty( parameters ) ) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -820,21 +820,17 @@ private static <F extends Annotation> void applyFilter(
applyAttributeIfSpecified( filterAnn, "condition", jaxbFilter.getCondition() );
applyAttributeIfSpecified( filterAnn, "deduceAliasInjectionPoints", jaxbFilter.isAutoAliasInjection() );

final List<Serializable> content = jaxbFilter.getContent();
if ( !isEmpty( content ) ) {
final List<MutableAnnotationUsage<SqlFragmentAlias>> aliases = arrayList( content.size() );
for ( Serializable c : content ) {
final JaxbHbmFilterImpl.JaxbAliasesImpl alias = (JaxbHbmFilterImpl.JaxbAliasesImpl) c;
final MutableAnnotationUsage<SqlFragmentAlias> aliasAnn = makeAnnotation(
SqlFragmentAlias.class,
null
);
final List<JaxbHbmFilterImpl.JaxbAliasesImpl> aliases = jaxbFilter.getAliases();
if ( !isEmpty( aliases ) ) {
final List<MutableAnnotationUsage<SqlFragmentAlias>> sqlFragmentAliases = arrayList( aliases.size() );
for ( JaxbHbmFilterImpl.JaxbAliasesImpl alias : aliases ) {
final MutableAnnotationUsage<SqlFragmentAlias> 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 );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -87,5 +91,17 @@ public boolean shouldIgnoreUnlistedClasses() {
final AnnotationUsage<Column> nameColumnAnn = nameAttribute.getMember().getAnnotationUsage( Column.class );
assertThat( nameColumnAnn ).isNotNull();
assertThat( nameColumnAnn.<String>getAttributeValue( "name" ) ).isEqualTo( "description" );

validateFilterUsage( root.getClassDetails().getAnnotationUsage( Filter.class ) );
}

private void validateFilterUsage(AnnotationUsage<Filter> filter) {
assertThat( filter ).isNotNull();
assertThat( filter.<String>getAttributeValue( "name" ) ).isEqualTo( "name_filter" );
assertThat( filter.<String>getAttributeValue( "condition" ) ).isEqualTo( "{t}.name = :name" );
final List<AnnotationUsage<SqlFragmentAlias>> aliases = filter.getAttributeValue( "aliases" );
assertThat( aliases ).hasSize( 1 );
assertThat( aliases.get( 0 ).<String>getAttributeValue( "alias" ) ).isEqualTo( "t" );
assertThat( aliases.get( 0 ).<String>getAttributeValue( "table" ) ).isEqualTo( "SimpleEntity" );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="3.1">
<entity class="org.hibernate.models.orm.xml.SimpleEntity" metadata-complete="true" access="FIELD">
<filter name="name_filter" condition="{t}.name = :name">
<aliases alias="t" table="SimpleEntity" />
</filter>

<attributes>
<id name="id">
<column name="pk"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2506,7 +2506,7 @@
<xsd:attribute name="name" use="required" type="xsd:string"/>
</xsd:complexType>

<xsd:complexType name="hbm-filter" mixed="true">
<xsd:complexType name="hbm-filter">
<xsd:annotation>
<xsd:documentation>
Applies a filter defined by hbm-filter-def usage
Expand All @@ -2525,14 +2525,22 @@
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="condition" type="xsd:string" />
</xsd:sequence>

<xsd:attribute name="name" use="required" type="xsd:string"/>
<xsd:attribute name="autoAliasInjection" type="xsd:boolean"/>
<xsd:attribute name="condition" type="xsd:string"/>
</xsd:complexType>

<!-- <xsd:complexType name="hbm-filter-alias" mixed="true">-->
<!-- <xsd:simpleContent>-->
<!-- <xsd:extension base="xsd:string">-->
<!-- <xsd:attribute name="alias" type="xsd:string" use="required"/>-->
<!-- <xsd:attribute name="table" type="xsd:string"/>-->
<!-- <xsd:attribute name="entity" type="xsd:string"/>-->
<!-- </xsd:extension>-->
<!-- </xsd:simpleContent>-->
<!-- </xsd:complexType>-->

<!-- **************************************************** -->

Expand Down
6 changes: 5 additions & 1 deletion hibernate-orm/src/main/xjb/mapping-bindings.xjb
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,11 @@
</bindings>
</bindings>


<bindings node="//xsd:complexType[@name='filter-def']">
<bindings node=".//xsd:element[@name='filter-param']">
<property name="filterParams"/>
</bindings>
</bindings>

<!--
#################################################################
Expand Down

0 comments on commit ea4e265

Please sign in to comment.