-->
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.  [ 3 posts ] 
Author Message
 Post subject: ICriteria + Projections + Strongly typed result
PostPosted: Mon Dec 04, 2006 6:12 am 
Beginner
Beginner

Joined: Fri Jul 29, 2005 10:34 am
Posts: 25
Hibernate version: 1.2 (beta 2)

Hi,
For projections + stronly typed results i was previously using SELECT NEW (hql) . Now i see that ICriteria also supports projections so i am trying to unify and use only criterias.

I have a simple POCO that maps the projected fields (i still want to have a strongly typed object to work with).

* using critera.List() --> returns a list of objects ( i do not know if there is a way to specify that the result should be of type "CustomView"..)

If I use the new syntax (generic) : i get an exception that System.Object can not be casted to my poco class (CustomView)
IList<CustomView> _cvs = _critera.List<CustomView>();

Make sense, the point is that i do not know if this is possible...

Below the code to use the new Projection stuff (great!)


Code:
public ICriteria GetBasicCriteria()
        {
            ICriteria _criteria = Session.CreateCriteria(typeof(Products)); if (_criteria == null) throw new Exception("Failed to create criteria for CustomView1");
           _criteria.CreateCriteria("suppliers","suppliers",NHibernate.SqlCommand.JoinType.LeftOuterJoin);
            _criteria.SetProjection( GetProjectionList() );

             return _criteria;
        }

    protected virtual IProjection GetProjectionList()
        {
            ProjectionList _properties = Projections.ProjectionList();
            _properties.Add(NHibernate.Expression.Property.ForName("productID"));
   _properties.Add(NHibernate.Expression.Property.ForName("productName"));
            _properties.Add(NHibernate.Expression.Property.ForName("supplierID"));
         
            _properties.Add(NHibernate.Expression.Property.ForName("suppliers.contactName"));
            _properties.Add(NHibernate.Expression.Property.ForName("suppliers.contactTitle"));

            return _properties;
        }


    Top
     Profile  
     
     Post subject:
    PostPosted: Mon Dec 04, 2006 6:50 am 
    Contributor
    Contributor

    Joined: Wed May 11, 2005 4:59 pm
    Posts: 1766
    Location: Prague, Czech Republic
    Use _criteria.SetResultTransformer(Transformers.AliasToBean(typeof(CustomView)));


    Top
     Profile  
     
     Post subject:
    PostPosted: Tue Dec 05, 2006 5:21 am 
    Beginner
    Beginner

    Joined: Fri Jul 29, 2005 10:34 am
    Posts: 25
    sergey wrote:
    Use _criteria.SetResultTransformer(Transformers.AliasToBean(typeof(CustomView)));


    Excellent feature. Worked perfectly .
    Thanks!


    Top
     Profile  
     
    Display posts from previous:  Sort by  
    Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.