Hi, I used the example from
http://liangwu.wordpress.com/2007/03/13/create-dto-with-nhibernate/
and i came up with this:
return session.CreateCriteria( typeof( SideFrame ) )
// where filter
.Add( Restrictions.Eq( "Size.Id", SizeID ) )
.Add( Restrictions.Eq( "Material.Id", MaterialID ) )
// map SideFrame properties to SideFrameDTO properties
.SetProjection( Projections.ProjectionList()
.Add( Projections.Property( "Id" ), "Id" )
.Add( Projections.Property( "PricePerUnit" ), "PricePerUnit" )
.Add( Projections.Property( "ArticleNumber" ), "ArticleNumber" ) )
// set the transformer
.SetResultTransformer( Transformers.AliasToBean( typeof
( SideFrameDTO ) ) )
// get data
.UniqueResult<SideFrameDTO>();
I didn't have to make an explicit ctor and Castle AR managed the xml.
And it works properly.
Thank you everybody for the pointers :))
On May 7, 5:06 pm, Anne Epstein <[email protected]> wrote:
> There's more than one way to get DTOs from NHibernate, but here's a
> way that's worked for me:
>
> First, I've successfully written that kind of query in this format:
> select distinct New NameValueDTO(p.Id, p.Name) from People p where
> p.Eyecolor = :color
> (You can also use the Criteria syntax and projections, as in, for
> instance,http://liangwu.wordpress.com/2007/03/13/create-dto-with-nhibernate/
> )
>
> Second, you'll need a constructor on your DTO that takes its
> constructor parameters in the order you used them in the query.
>
> Last, you'll need to let NHibernate know about DTO classes used as
> query results. You don't need to do a full mapping, but you can do
> something like the following:
>
> <?xml version="1.0" encoding="utf-8" ?>
> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
> <import class="MyProject.Domain.NameValueDTO, MyProject.Domain"/>
> <import class="MyProject.Domain.OtherDTO,MyProject.Domain"/>
> </hibernate-mapping>
>
> Good luck!
>
> On Thu, May 7, 2009 at 7:22 AM, bdaniel7 <[email protected]> wrote:
>
> > Hello, I'm trying to load 3 properties, from a table SideFrames.
> > (I have a class SideFrame also, mapped to the table.)
> > For this I created a class SideFrameDTO, with Id, Price, ArtNo.
> > I want to execute the query below and get an instance of SideFrameDTO.
>
> > But I get "NHibernate.MappingException: No persister for:
> > SideFrameDTO"
> > I believe because the DTO class is not mapped.
>
> > But, is it possible to do this (only 3 props for a smaller class)?
>
> > Thank you,
> > Dan
>
> > string query = @"select
> > p.Id as {sideFrameDTO.Id},
> > p.PricePerUnit as {sideFrameDTO.Price},
> > p.ArticleNumber as {sideFrameDTO.ArtNo}
> > from SideFrames sf join Products p on p.Id = sf.SideFrameId
> > where sf.Size = :sizeID and sf.Material = :materialID";
>
> > IQuery sqlQuery = session.CreateSQLQuery( query )
> > .AddEntity( "sideFrameDTO", typeof ( SideFrameDTO ) );
>
> > sqlQuery.SetParameter( "sizeID", SizeID );
> > sqlQuery.SetParameter( "materialID", MaterialID );
>
> > sqlQuery.SetMaxResults( 1 );
> > return sqlQuery.UniqueResult< SideFrameDTO >();
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---