If using detachedCriteria for subqueries the subquery do not use activated filters.
Beneath you might find a standalone application to reproduce it.
Its output are 3 select statements where the 3rd is the one with the subquery.
Note: Since annotations do not allow to define filter I configured them in code.
Hibernate version:
hibernate3 cvs 2005-03-18
annotations cvs 2005-03-18
Code:
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.Hibernate;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Property;
import org.hibernate.engine.FilterDefinition;
import org.hibernate.tool.hbm2ddl.SchemaUpdate;
import org.hibernate.dialect.HSQLDialect;
import javax.persistence.AccessType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.OneToOne;
import javax.persistence.Id;
import javax.persistence.GeneratorType;
import javax.persistence.Embeddable;
import java.util.List;
import java.util.Iterator;
import java.io.Serializable;
public class HbmP1
{
@Entity(access = AccessType.FIELD)
public static class TableA implements Serializable
{
@Id(generate=GeneratorType.NONE)
private TableAPk key;
private String value;
public TableA()
{
}
public TableAPk getKey()
{
return key;
}
public void setKey(TableAPk key)
{
this.key = key;
}
public String getValue()
{
return value;
}
public void setValue(String value)
{
this.value = value;
}
public boolean equals(Object o)
{
if (this == o)
{
return true;
}
if (!(o instanceof TableA))
{
return false;
}
final TableA tableA = (TableA) o;
if (key != null ? !key.equals(tableA.key) : tableA.key != null)
{
return false;
}
return true;
}
public int hashCode()
{
return (key != null ? key.hashCode() : 0);
}
}
@Embeddable(access=AccessType.FIELD)
public static class TableAPk implements Serializable
{
private String companyId;
private String customerId;
public TableAPk()
{
}
public String getCompanyId()
{
return companyId;
}
public void setCompanyId(String companyId)
{
this.companyId = companyId;
}
public String getCustomerId()
{
return customerId;
}
public void setCustomerId(String customerId)
{
this.customerId = customerId;
}
public boolean equals(Object o)
{
if (this == o)
{
return true;
}
if (!(o instanceof TableAPk))
{
return false;
}
final TableAPk tableAPk = (TableAPk) o;
if (companyId != null ? !companyId.equals(tableAPk.companyId) : tableAPk.companyId != null)
{
return false;
}
if (customerId != null ? !customerId.equals(tableAPk.customerId) : tableAPk.customerId != null)
{
return false;
}
return true;
}
public int hashCode()
{
int result;
result = (companyId != null ? companyId.hashCode() : 0);
result = 29 * result + (customerId != null ? customerId.hashCode() : 0);
return result;
}
}
@Entity(access = AccessType.FIELD)
public static class TableB implements Serializable
{
@Id(generate=GeneratorType.NONE)
private TableBPk key;
private String value;
public TableB()
{
}
public TableBPk getKey()
{
return key;
}
public void setKey(TableBPk key)
{
this.key = key;
}
public String getValue()
{
return value;
}
public void setValue(String value)
{
this.value = value;
}
public boolean equals(Object o)
{
if (this == o)
{
return true;
}
if (!(o instanceof TableB))
{
return false;
}
final TableB tableB = (TableB) o;
if (key != null ? !key.equals(tableB.key) : tableB.key != null)
{
return false;
}
return true;
}
public int hashCode()
{
return (key != null ? key.hashCode() : 0);
}
}
@Embeddable(access=AccessType.FIELD)
public static class TableBPk implements Serializable
{
private String companyId;
private String customerId;
public TableBPk()
{
}
public String getCompanyId()
{
return companyId;
}
public void setCompanyId(String companyId)
{
this.companyId = companyId;
}
public String getCustomerId()
{
return customerId;
}
public void setCustomerId(String customerId)
{
this.customerId = customerId;
}
public boolean equals(Object o)
{
if (this == o)
{
return true;
}
if (!(o instanceof TableBPk))
{
return false;
}
final TableBPk tableBPk = (TableBPk) o;
if (companyId != null ? !companyId.equals(tableBPk.companyId) : tableBPk.companyId != null)
{
return false;
}
if (customerId != null ? !customerId.equals(tableBPk.customerId) : tableBPk.customerId != null)
{
return false;
}
return true;
}
public int hashCode()
{
int result;
result = (companyId != null ? companyId.hashCode() : 0);
result = 29 * result + (customerId != null ? customerId.hashCode() : 0);
return result;
}
}
public static void main(String[] args)
{
AnnotationConfiguration cfg = new AnnotationConfiguration();
cfg.addAnnotatedClass(TableA.class);
cfg.addAnnotatedClass(TableB.class);
cfg.getClassMapping(TableA.class.getName()).addFilter("currCompanyOnly", ":currCompany = companyId");
cfg.getClassMapping(TableB.class.getName()).addFilter("currCompanyOnly", ":currCompany = companyId");
FilterDefinition currCompanyOnly = new FilterDefinition("currCompanyOnly");
currCompanyOnly.addParameterType("currCompany", Hibernate.STRING);
cfg.addFilterDefinition(currCompanyOnly);
cfg.setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver");
cfg.setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:hbmtest");
cfg.setProperty("hibernate.dialect", HSQLDialect.class.getName());
cfg.setProperty("hibernate.show_sql", "true");
SessionFactory factory = cfg.buildSessionFactory();
SchemaUpdate update = new SchemaUpdate(cfg);
update.execute(false, true);
Session session = factory.openSession();
session.enableFilter("currCompanyOnly").setParameter("currCompany", "A1");
// uses filter
session.createCriteria(TableA.class).list();
// uses filter
session.createCriteria(TableB.class).list();
// does not use filter for criteria A (subquery)
DetachedCriteria critA = DetachedCriteria.forClass(TableA.class);
critA.setProjection(Property.forName("key.customerId"));
DetachedCriteria critB = DetachedCriteria.forClass(TableB.class);
critB.add(Property.forName("key.customerId").eq(critA));
critB.getExecutableCriteria(session).list();
}
}