-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: subquery do not take filter into account
PostPosted: Fri Mar 18, 2005 6:10 am 
Regular
Regular

Joined: Thu Dec 11, 2003 4:14 pm
Posts: 86
Location: Hibernate 3 + annotations, Oracle 9i, PostgreSQL 8.0, Java 1.5.0
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();
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 18, 2005 6:12 am 
Regular
Regular

Joined: Thu Dec 11, 2003 4:14 pm
Posts: 86
Location: Hibernate 3 + annotations, Oracle 9i, PostgreSQL 8.0, Java 1.5.0
JIRA: http://opensource.atlassian.com/project ... se/HHH-254


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.