Author: sharathjreddy
Date: 2009-10-28 06:57:21 -0400 (Wed, 28 Oct 2009)
New Revision: 17859
Added:
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/entity/Drill.java
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/entity/PowerDrill.java
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/entity/Tool.java
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/entity/Widget.java
Modified:
core/trunk/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/entity/BasicHibernateAnnotationsTest.java
Log:
HHH-4332 Filters for MappedSuperClass
Modified: core/trunk/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java
===================================================================
---
core/trunk/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java 2009-10-28
10:45:31 UTC (rev 17858)
+++
core/trunk/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java 2009-10-28
10:57:21 UTC (rev 17859)
@@ -434,6 +434,7 @@
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 @@
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 are not allowed on subclasses
+ if ( !inheritanceState.hasParents ) {
+ bindFilters(clazzToProcess, entityBinder, mappings);
}
- Filters filtersAnn = annotatedClass.getAnnotation( Filters.class );
- if ( filtersAnn != null ) {
- for (Filter filter : filtersAnn.value()) {
- entityBinder.addFilter( filter.name(), filter.condition() );
- }
- }
+
entityBinder.bindEntity();
if ( inheritanceState.hasTable() ) {
@@ -969,7 +966,42 @@
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 );
Modified:
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/entity/BasicHibernateAnnotationsTest.java
===================================================================
---
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/entity/BasicHibernateAnnotationsTest.java 2009-10-28
10:45:31 UTC (rev 17858)
+++
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/entity/BasicHibernateAnnotationsTest.java 2009-10-28
10:57:21 UTC (rev 17859)
@@ -341,7 +341,54 @@
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 @@
super( x );
}
- protected Class[] getMappings() {
+ protected Class<?>[] getMappings() {
return new Class[]{
Forest.class,
Tree.class,
@@ -532,7 +579,9 @@
Peugot.class,
ContactDetails.class,
Topic.class,
- Narrative.class
+ Narrative.class,
+ Drill.class,
+ PowerDrill.class
};
}
Added:
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/entity/Drill.java
===================================================================
--- core/trunk/annotations/src/test/java/org/hibernate/test/annotations/entity/Drill.java
(rev 0)
+++
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/entity/Drill.java 2009-10-28
10:57:21 UTC (rev 17859)
@@ -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
+(a)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;
+ }
+
+}
Added:
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/entity/PowerDrill.java
===================================================================
---
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/entity/PowerDrill.java
(rev 0)
+++
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/entity/PowerDrill.java 2009-10-28
10:57:21 UTC (rev 17859)
@@ -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 {
+
+}
Added:
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/entity/Tool.java
===================================================================
--- core/trunk/annotations/src/test/java/org/hibernate/test/annotations/entity/Tool.java
(rev 0)
+++
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/entity/Tool.java 2009-10-28
10:57:21 UTC (rev 17859)
@@ -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;
+ }
+
+
+}
Added:
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/entity/Widget.java
===================================================================
---
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/entity/Widget.java
(rev 0)
+++
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/entity/Widget.java 2009-10-28
10:57:21 UTC (rev 17859)
@@ -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;
+ }
+
+}