HHH-2394 Got filters working with secondary tables

This commit is contained in:
Rob Worsnop 2012-06-28 09:38:25 -04:00 committed by Strong Liu
parent 05dcc209ae
commit 1cd8db2ac3
7 changed files with 212 additions and 14 deletions

View File

@ -0,0 +1,52 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat Inc. 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.internal;
import org.hibernate.persister.entity.AbstractEntityPersister;
/**
*
* @author Rob Worsnop
*
*/
public class DynamicFilterAliasGenerator implements FilterAliasGenerator {
private String[] tables;
private String rootAlias;
public DynamicFilterAliasGenerator(String[] tables, String rootAlias) {
this.tables = tables;
this.rootAlias = rootAlias;
}
@Override
public String getAlias(String table) {
if (table == null){
return rootAlias;
} else{
return AbstractEntityPersister.generateTableAlias(rootAlias, AbstractEntityPersister.getTableId(table, tables));
}
}
}

View File

@ -84,7 +84,6 @@ import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.FilterAliasGenerator;
import org.hibernate.internal.FilterConfiguration;
import org.hibernate.internal.FilterHelper;
import org.hibernate.internal.StaticFilterAliasGenerator;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.jdbc.Expectation;
@ -1892,7 +1891,7 @@ public abstract class AbstractEntityPersister
};
}
protected String generateTableAlias(String rootAlias, int tableNumber) {
public static String generateTableAlias(String rootAlias, int tableNumber) {
if ( tableNumber == 0 ) {
return rootAlias;
}
@ -4736,10 +4735,15 @@ public abstract class AbstractEntityPersister
getEntityTuplizer().setPropertyValue( object, propertyName, value );
}
public FilterAliasGenerator getFilterAliasGenerator(final String rootAlias){
return new StaticFilterAliasGenerator(rootAlias);
public static int getTableId(String tableName, String[] tables) {
for ( int j = 0; j < tables.length; j++ ) {
if ( tableName.equalsIgnoreCase( tables[j] ) ) {
return j;
}
}
throw new AssertionFailure( "Table " + tableName + " not found" );
}
@Override
public EntityMode getEntityMode() {
return entityMetamodel.getEntityMode();
@ -4763,4 +4767,5 @@ public abstract class AbstractEntityPersister
public int determineTableNumberForColumn(String columnName) {
return 0;
}
}

View File

@ -39,6 +39,7 @@ import org.hibernate.engine.OptimisticLockStyle;
import org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle;
import org.hibernate.engine.spi.Mapping;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.internal.DynamicFilterAliasGenerator;
import org.hibernate.internal.FilterAliasGenerator;
import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.mapping.Column;
@ -695,15 +696,6 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
return tableNames[0];
}
private static int getTableId(String tableName, String[] tables) {
for ( int j = 0; j < tables.length; j++ ) {
if ( tableName.equals( tables[j] ) ) {
return j;
}
}
throw new AssertionFailure( "Table " + tableName + " not found" );
}
public void addDiscriminatorToSelect(SelectFragment select, String name, String suffix) {
if ( hasSubclasses() ) {
select.setExtraSelectList( discriminatorFragment( name ), getDiscriminatorAlias() );
@ -875,5 +867,8 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
}
}
};
@Override
public FilterAliasGenerator getFilterAliasGenerator(String rootAlias) {
return new DynamicFilterAliasGenerator(subclassTableNameClosure, rootAlias);
}
}

View File

@ -37,6 +37,8 @@ import org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy;
import org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle;
import org.hibernate.engine.spi.Mapping;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.internal.DynamicFilterAliasGenerator;
import org.hibernate.internal.FilterAliasGenerator;
import org.hibernate.internal.util.MarkerObject;
import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.mapping.Column;
@ -1037,4 +1039,9 @@ public class SingleTableEntityPersister extends AbstractEntityPersister {
public String[][] getContraintOrderedTableKeyColumnClosure() {
return constraintOrderedKeyColumnNames;
}
@Override
public FilterAliasGenerator getFilterAliasGenerator(String rootAlias) {
return new DynamicFilterAliasGenerator(qualifiedTableNames, rootAlias);
}
}

View File

@ -43,6 +43,8 @@ import org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle;
import org.hibernate.engine.spi.Mapping;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.id.IdentityGenerator;
import org.hibernate.internal.FilterAliasGenerator;
import org.hibernate.internal.StaticFilterAliasGenerator;
import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.internal.util.collections.JoinedIterator;
import org.hibernate.internal.util.collections.SingletonIterator;
@ -507,4 +509,9 @@ public class UnionSubclassEntityPersister extends AbstractEntityPersister {
public String[][] getContraintOrderedTableKeyColumnClosure() {
return constraintOrderedKeyColumnNames;
}
@Override
public FilterAliasGenerator getFilterAliasGenerator(String rootAlias) {
return new StaticFilterAliasGenerator(rootAlias);
}
}

View File

@ -0,0 +1,41 @@
package org.hibernate.test.annotations.filter.secondarytable;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Assert;
import org.junit.Test;
public class SecondaryTableTest extends BaseCoreFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] {User.class};
}
@Override
protected void prepareTest() throws Exception {
openSession();
insertUser("q@s.com", 21, false, "a1", "b");
insertUser("r@s.com", 22, false, "a2", "b");
insertUser("s@s.com", 23, true, "a3", "b");
insertUser("t@s.com", 24, false, "a4", "b");
session.flush();
}
@Test
public void testFilter(){
Assert.assertEquals(Long.valueOf(4), session.createQuery("select count(u) from User u").uniqueResult());
session.enableFilter("ageFilter").setParameter("age", 24);
Assert.assertEquals(Long.valueOf(2), session.createQuery("select count(u) from User u").uniqueResult());
}
private void insertUser(String emailAddress, int age, boolean lockedOut, String username, String password){
User user = new User();
user.setEmailAddress(emailAddress);
user.setAge(age);
user.setLockedOut(lockedOut);
user.setUsername(username);
user.setPassword(password);
session.persist(user);
}
}

View File

@ -0,0 +1,91 @@
package org.hibernate.test.annotations.filter.secondarytable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.SecondaryTable;
import javax.persistence.Table;
import org.hibernate.annotations.Filter;
import org.hibernate.annotations.FilterDef;
import org.hibernate.annotations.ParamDef;
import org.hibernate.annotations.SqlFragmentAlias;
@Entity
@Table(name="USER")
@SecondaryTable(name="SECURITY_USER")
@FilterDef(name="ageFilter", parameters=@ParamDef(name="age", type="integer"))
@Filter(name="ageFilter", condition="{u}.AGE < :age AND {s}.LOCKED_OUT <> 1",
aliases={@SqlFragmentAlias(alias="u", table="USER"), @SqlFragmentAlias(alias="s", table="SECURITY_USER")})
public class User {
@Id
@GeneratedValue
@Column(name="USER_ID")
private int id;
@Column(name="EMAIL_ADDRESS")
private String emailAddress;
@Column(name="AGE")
private int age;
@Column(name="USERNAME", table="SECURITY_USER")
private String username;
@Column(name="PASSWORD", table="SECURITY_USER")
private String password;
@Column(name="LOCKED_OUT", table="SECURITY_USER")
private boolean lockedOut;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isLockedOut() {
return lockedOut;
}
public void setLockedOut(boolean lockedOut) {
this.lockedOut = lockedOut;
}
}