HHH-4332 Filters for MappedSuperClass
git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@17859 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
parent
356063a33f
commit
a1e8d7cb0d
|
@ -434,6 +434,7 @@ public final class AnnotationBinder {
|
|||
if ( AnnotatedClassType.EMBEDDABLE_SUPERCLASS.equals( classType ) ) {
|
||||
bindQueries( clazzToProcess, mappings );
|
||||
bindTypeDefs(clazzToProcess, mappings);
|
||||
bindFilterDefs(clazzToProcess, mappings);
|
||||
}
|
||||
|
||||
if ( AnnotatedClassType.EMBEDDABLE_SUPERCLASS.equals( classType ) //will be processed by their subentities
|
||||
|
@ -593,16 +594,12 @@ public final class AnnotationBinder {
|
|||
entityBinder.setWhere( whereAnn );
|
||||
entityBinder.setCache( cacheAnn );
|
||||
entityBinder.setInheritanceState( inheritanceState );
|
||||
Filter filterAnn = annotatedClass.getAnnotation( Filter.class );
|
||||
if ( filterAnn != null ) {
|
||||
entityBinder.addFilter( filterAnn.name(), filterAnn.condition() );
|
||||
}
|
||||
Filters filtersAnn = annotatedClass.getAnnotation( Filters.class );
|
||||
if ( filtersAnn != null ) {
|
||||
for (Filter filter : filtersAnn.value()) {
|
||||
entityBinder.addFilter( filter.name(), filter.condition() );
|
||||
}
|
||||
|
||||
//Filters are not allowed on subclasses
|
||||
if ( !inheritanceState.hasParents ) {
|
||||
bindFilters(clazzToProcess, entityBinder, mappings);
|
||||
}
|
||||
|
||||
entityBinder.bindEntity();
|
||||
|
||||
if ( inheritanceState.hasTable() ) {
|
||||
|
@ -969,7 +966,42 @@ public final class AnnotationBinder {
|
|||
|
||||
return classesToProcess;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Process the filters defined on the given class, as well as all filters defined
|
||||
* on the MappedSuperclass(s) in the inheritance hierarchy
|
||||
*/
|
||||
private static void bindFilters(XClass annotatedClass, EntityBinder entityBinder,
|
||||
ExtendedMappings mappings) {
|
||||
|
||||
bindFilters(annotatedClass, entityBinder);
|
||||
|
||||
XClass classToProcess = annotatedClass.getSuperclass();
|
||||
while (classToProcess != null) {
|
||||
AnnotatedClassType classType = mappings.getClassType( classToProcess );
|
||||
if ( AnnotatedClassType.EMBEDDABLE_SUPERCLASS.equals( classType ) ) {
|
||||
bindFilters(classToProcess, entityBinder);
|
||||
}
|
||||
classToProcess = classToProcess.getSuperclass();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static void bindFilters(XAnnotatedElement annotatedElement, EntityBinder entityBinder) {
|
||||
|
||||
Filters filtersAnn = annotatedElement.getAnnotation( Filters.class );
|
||||
if ( filtersAnn != null ) {
|
||||
for (Filter filter : filtersAnn.value()) {
|
||||
entityBinder.addFilter( filter.name(), filter.condition() );
|
||||
}
|
||||
}
|
||||
|
||||
Filter filterAnn = annotatedElement.getAnnotation( Filter.class );
|
||||
if ( filterAnn != null ) {
|
||||
entityBinder.addFilter( filterAnn.name(), filterAnn.condition() );
|
||||
}
|
||||
}
|
||||
|
||||
private static void bindFilterDefs(XAnnotatedElement annotatedElement, ExtendedMappings mappings) {
|
||||
FilterDef defAnn = annotatedElement.getAnnotation( FilterDef.class );
|
||||
FilterDefs defsAnn = annotatedElement.getAnnotation( FilterDefs.class );
|
||||
|
|
|
@ -341,7 +341,54 @@ public class BasicHibernateAnnotationsTest extends TestCase {
|
|||
tx.rollback();
|
||||
s.close();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests the functionality of inheriting @Filter and @FilterDef annotations
|
||||
* defined on a parent MappedSuperclass(s)
|
||||
*/
|
||||
public void testInheritFiltersFromMappedSuperclass() throws Exception {
|
||||
Session s;
|
||||
Transaction tx;
|
||||
s = openSession();
|
||||
tx = s.beginTransaction();
|
||||
s.createQuery( "delete Drill" ).executeUpdate();
|
||||
Drill d1 = new PowerDrill();
|
||||
d1.setName("HomeDrill1");
|
||||
d1.setCategory("HomeImprovment");
|
||||
s.persist( d1 );
|
||||
Drill d2 = new PowerDrill();
|
||||
d2.setName("HomeDrill2");
|
||||
d2.setCategory("HomeImprovement");
|
||||
s.persist(d2);
|
||||
Drill d3 = new PowerDrill();
|
||||
d3.setName("HighPowerDrill");
|
||||
d3.setCategory("Industrial");
|
||||
s.persist( d3 );
|
||||
tx.commit();
|
||||
s.close();
|
||||
s = openSession();
|
||||
tx = s.beginTransaction();
|
||||
|
||||
//We test every filter with 2 queries, the first on the base class of the
|
||||
//inheritance hierarchy (Drill), and the second on a subclass (PowerDrill)
|
||||
s.enableFilter( "byName" ).setParameter( "name", "HomeDrill1");
|
||||
long count = ( (Long) s.createQuery( "select count(*) from Drill" ).iterate().next() ).intValue();
|
||||
assertEquals( 1, count );
|
||||
count = ( (Long) s.createQuery( "select count(*) from PowerDrill" ).iterate().next() ).intValue();
|
||||
assertEquals( 1, count );
|
||||
s.disableFilter( "byName" );
|
||||
|
||||
s.enableFilter( "byCategory" ).setParameter( "category", "Industrial" );
|
||||
count = ( (Long) s.createQuery( "select count(*) from Drill" ).iterate().next() ).longValue();
|
||||
assertEquals( 1, count );
|
||||
count = ( (Long) s.createQuery( "select count(*) from PowerDrill" ).iterate().next() ).longValue();
|
||||
assertEquals( 1, count );
|
||||
s.disableFilter( "byCategory" );
|
||||
|
||||
tx.rollback();
|
||||
s.close();
|
||||
}
|
||||
|
||||
public void testParameterizedType() throws Exception {
|
||||
if( !getDialect().supportsExpectedLobUsagePattern() ){
|
||||
return;
|
||||
|
@ -519,7 +566,7 @@ public class BasicHibernateAnnotationsTest extends TestCase {
|
|||
super( x );
|
||||
}
|
||||
|
||||
protected Class[] getMappings() {
|
||||
protected Class<?>[] getMappings() {
|
||||
return new Class[]{
|
||||
Forest.class,
|
||||
Tree.class,
|
||||
|
@ -532,7 +579,9 @@ public class BasicHibernateAnnotationsTest extends TestCase {
|
|||
Peugot.class,
|
||||
ContactDetails.class,
|
||||
Topic.class,
|
||||
Narrative.class
|
||||
Narrative.class,
|
||||
Drill.class,
|
||||
PowerDrill.class
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2009, Red Hat, Inc. and/or its affiliates or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat, Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package org.hibernate.test.annotations.entity;
|
||||
|
||||
import javax.persistence.DiscriminatorColumn;
|
||||
import javax.persistence.DiscriminatorType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Inheritance;
|
||||
import javax.persistence.InheritanceType;
|
||||
|
||||
/**
|
||||
* @author Sharath Reddy
|
||||
*/
|
||||
@Entity
|
||||
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
|
||||
@DiscriminatorColumn(
|
||||
name="DRILL_TYPE",
|
||||
discriminatorType = DiscriminatorType.STRING
|
||||
)
|
||||
public abstract class Drill extends Tool {
|
||||
|
||||
@Id @GeneratedValue
|
||||
private int id;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2009, Red Hat, Inc. and/or its affiliates or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat, Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package org.hibernate.test.annotations.entity;
|
||||
|
||||
import javax.persistence.DiscriminatorValue;
|
||||
import javax.persistence.Entity;
|
||||
|
||||
/**
|
||||
* @author Sharath Reddy
|
||||
*/
|
||||
@Entity
|
||||
@DiscriminatorValue("Power")
|
||||
public class PowerDrill extends Drill {
|
||||
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2009, Red Hat, Inc. and/or its affiliates or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat, Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package org.hibernate.test.annotations.entity;
|
||||
|
||||
import javax.persistence.MappedSuperclass;
|
||||
import org.hibernate.annotations.Filter;
|
||||
import org.hibernate.annotations.FilterDef;
|
||||
import org.hibernate.annotations.ParamDef;
|
||||
|
||||
/**
|
||||
* @author Sharath Reddy
|
||||
*/
|
||||
@FilterDef(name = "byCategory", parameters = {@ParamDef(name = "category", type = "string")})
|
||||
@Filter(name = "byCategory", condition = ":category = CATEGORY")
|
||||
@MappedSuperclass
|
||||
public class Tool extends Widget {
|
||||
|
||||
private String category;
|
||||
|
||||
public String getCategory() {
|
||||
return category;
|
||||
}
|
||||
|
||||
public void setCategory(String category) {
|
||||
this.category = category;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2009, Red Hat, Inc. and/or its affiliates or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat, Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package org.hibernate.test.annotations.entity;
|
||||
|
||||
import javax.persistence.MappedSuperclass;
|
||||
import org.hibernate.annotations.Filter;
|
||||
import org.hibernate.annotations.FilterDef;
|
||||
import org.hibernate.annotations.ParamDef;
|
||||
|
||||
/**
|
||||
* @author Sharath Reddy
|
||||
*/
|
||||
@FilterDef(name = "byName", parameters = {@ParamDef(name = "name", type = "string")})
|
||||
@Filter(name = "byName", condition = ":name = name")
|
||||
@MappedSuperclass
|
||||
public class Widget {
|
||||
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue