-->
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: criteria restriction for "in" to use object refere
PostPosted: Mon Jun 12, 2006 3:40 am 
Senior
Senior

Joined: Sat Jul 17, 2004 5:16 pm
Posts: 143
I am trying to do a criteria join query, and I would like an "in" clause which works on the original object. The only way I can get in to work is by fields. Can someone please help? You see below where I try to do an "in" by the alias of the original object, you can see the stacktrace that this does not work.

Thanks,
Chris


Hibernate version:
3.1

Code between sessionFactory.openSession() and session.close():
Code:
      CourtCase courtCase = new CourtCaseDAO().findByPK(new Integer(86));
      CourtCase courtCase2 = new CourtCaseDAO().findByPK(new Integer(8031));

      final List someList = new ArrayList();

      someList.add(courtCase);
      someList.add(courtCase2);

      final ProjectionList projectionList = Projections.projectionList();
      projectionList.add(Projections.property("sub__.gender"));
      projectionList.add(Projections.property("sub__.race"));
      projectionList.add(Projections.property("sub__.dob"));
      projectionList.add(Projections.property("sub__.clientId"));

      List theResults = (List)HibernateHelper.callbackHibernateSessionUnchecked(new HibernateHandler() {

         public Object callback(Session hibernateSession) throws Exception {
            
            Criteria criteria = hibernateSession.createCriteria(CourtCase.class, "courtCase__");
            criteria.createCriteria("client", "sub__");
            criteria.setProjection(projectionList);
            criteria.add(Restrictions.in("courtCase__", someList));
            return criteria.list();
         }
         
      });


Full stack trace of any exception that occurs:

testInitObjectFieldOneToMany(gov.me.bds.osa.drugcourt.util.HibernateHelperTest)org.hibernate.QueryException: could not resolve property: courtCase__ of: gov.me.bds.osa.drugcourt.model.CourtCase
at org.hibernate.persister.entity.AbstractPropertyMapping.throwPropertyException(AbstractPropertyMapping.java:43)
at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:37)
at org.hibernate.persister.entity.AbstractEntityPersister.getSubclassPropertyTableNumber(AbstractEntityPersister.java:1282)
at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:31)
at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1257)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:433)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumnsUsingProjection(CriteriaQueryTranslator.java:393)
at org.hibernate.criterion.InExpression.toSqlString(InExpression.java:35)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:333)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:71)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:67)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1514)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
at gov.me.bds.osa.drugcourt.util.HibernateHelperTest$1.callback(HibernateHelperTest.java:103)
at gov.me.bds.osa.drugcourt.util.HibernateHelper.callbackHibernateSession(HibernateHelper.java:1219)
at gov.me.bds.osa.drugcourt.util.HibernateHelper.callbackHibernateSession(HibernateHelper.java:1183)
at gov.me.bds.osa.drugcourt.util.HibernateHelper.callbackHibernateSessionUnchecked(HibernateHelper.java:1163)
at gov.me.bds.osa.drugcourt.util.HibernateHelperTest.testInitObjectFieldOneToMany(HibernateHelperTest.java:95)

Name and version of the database you are using:
Oracle
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 12, 2006 1:06 pm 
Senior
Senior

Joined: Sat Jul 17, 2004 5:16 pm
Posts: 143
I coded a workaround, though I think there might be a way to do it in hibernate if I knew the syntax...

I made a factory method to return a criterion (not all code here, but the gist is here)

The end result is something like this:

Code:
Expression.sql("{alias}.id in(?,?)", courtCaseArray, new Type[]{Hibernate.entity(CourtCase.class), Hibernate.entity(CourtCase.class)})


The method is here

Code:
   /**
    *
    * @param instancesOrListOrArray
    * @return the crinterion or null if nothing passed in
    */
   public static Criterion restrictionAliasClassIn(Object instancesOrListOrArray) {
      
      Object[] instancesArray = PwwObjectUtils.convertObjectOrListOrArrayToArray(instancesOrListOrArray);
      int instancesLength = PwwObjectUtils.size(instancesArray);
      if (instancesLength == 0) {
         return null;
      }
      //assume all same type
      Class typeClass = PwwObjectUtils.getClass(instancesArray[0]);
      String primaryKey = HibernateHelper.columnNamePrimaryKey(typeClass);
      //make the in string: {alias}.id in(?,?)
      StringBuffer inString = new StringBuffer("{alias}.");
      inString.append(primaryKey).append(" in(");
      Type[] types = new Type[instancesLength];
      Type type = Hibernate.entity(typeClass);
      for (int i=0;i<instancesLength;i++) {
         inString.append("?");
         if (i <instancesLength-1) {
            inString.append(",");
         }
         types[i]=type;
      }
      inString.append(")");
      return Expression.sql(inString.toString(), instancesArray, types);
   }


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.