-->
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 projection should return the object itself
PostPosted: Mon Jun 12, 2006 3:33 am 
Senior
Senior

Joined: Sat Jul 17, 2004 5:16 pm
Posts: 143
I am doing a criteria projection query which joins two tables. this is a little weird, but I also want the object of the original criteria. The only Projection I can see to use is a field of the original object, but not the object itself. I need a "this" or something keyword or new projection type. I see I can get the id of the object, but I would like the object itself.

Thanks,
Chris

ps. below I try to get the alias of the object, which says obviously it cant find the field...

Hibernate version:
3.1

Code between sessionFactory.openSession() and session.close():

Code:
      final ProjectionList projectionList = Projections.projectionList();
      projectionList.add(Projections.property("courtCase__"));
      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.toType(AbstractEntityPersister.java:1265)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getType(CriteriaQueryTranslator.java:476)
at org.hibernate.criterion.PropertyProjection.getTypes(PropertyProjection.java:36)
at org.hibernate.criterion.ProjectionList.getTypes(ProjectionList.java:38)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getProjectedTypes(CriteriaQueryTranslator.java:317)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:69)
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:104)
at gov.me.bds.osa.drugcourt.util.HibernateHelper.callbackHibernateSession(HibernateHelper.java:1219)
at gov.me.bds.osa.drugcourt.util.HibernateHelper.callbackHibernateSession(HibernateHelper.java:1183)

Name and version of the database you are using:
Oracle


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 12, 2006 2:31 pm 
Senior
Senior

Joined: Sat Jul 17, 2004 5:16 pm
Posts: 143
I found a workaround for this too doing SQL projections. I must say the SQL projections are pretty darn powerful.

The end result I needed was something like this:

Code:
projectionList.add(Projections.sqlProjection("{alias}.id theObj", new String[]{"theObj"},
      new Type[]{Hibernate.entity(CourtCase.class)}));


So I made a factory method to create these (not all code provided, but you get the idea)

Code:
   /**
    * generate a projection for the object in question
    * @param theClass
    * @return a projection
    */
   public static Projection thisObjectProjection(Class theClass) {
      //Projections.sqlProjection("{alias}.id theObj__", new String[]{"theObj__"},
      //      new Type[]{Hibernate.entity(CourtCase.class)}));
      String primaryKeyColumn = HibernateHelper.columnNamePrimaryKey(theClass);
      Projection projection = Projections.sqlProjection(
            "{alias}." + primaryKeyColumn + " theObj__", new String[]{"theObj__"},
                  new Type[]{Hibernate.entity(theClass)});
      return projection;
   }


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.