Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8976283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:05:43+00:00 2026-06-15T19:05:43+00:00

Is it OK to create Mappings for DTO objects and then query them instead

  • 0
  1. Is it OK to create Mappings for DTO objects and then query them
    instead of domain? If it is not explain why?
  2. What If i need couple of those dtos?

  • DTos are readonly
  • ID is autogenerated by NH
  • In future these dtos will have set mappings to linked dtos.
  • I use DTO to reduce query size

    <class name="Person" table="`APP_Person`">  
        <property name="FirstName" type="string" length="512" />
        <property name="Age" type="int" /> 
        <property name="SocialNumber" type="int" />
        <property name="PassportId" type="int" />
        <property name="Salary" type="int" />
    </class>
    
    <class  name="PersonDTO" table="`APP_Person`">  
        <property name="FirstName" type="string" length="512" />
        <property name="Age" type="int" />
    </class>
    
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:05:45+00:00Added an answer on June 15, 2026 at 7:05 pm

    You don’t need to map/persist a DTO object. It’s normaly to readonly data and send to other layer of your application (web services, views, etc…).

    You can create a query on the Person entity that returns a PersonDTO list. Take a look at SetResultTransformer method. Try somethin like this:

    var query = session.CreateQuery("select p.FirstName, p.Age from Person p order by p.FirstName")
                       .SetResultTransformer(NHibernate.Transform.Transformers.AliasToBean(typeof(PersonDTO)))
                       .List<PersonDTO>();
    

    And your DTO:

    public class PersonDTO 
    {
       public string FirstName { get; set; }
       public int Age { get; set; }
    }
    

    The result of the column on the hql query should have the same name of your DTO’s properties to NHibernate do the right reflection when construct the DTO and hydrate the object.

    Linq

    You also can use linq to have a DTO (or a list of DTOs) as a result. For sample:

    var query = Session.Query<Person>().OrderBy(x => x.FirstName)
                       .Select(x = new PersonDTO() { FirstName = x.FirstName, Age = x.Age })
                       .ToList();
    

    Look this article: http://gustavoringel.blogspot.com.br/2009/02/creating-dto-in-nhibernate-hql-using.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create DTOs from NHibernate POCO objects. The problem is that the
I've seen a lot of questions related to mapping DTOs to Domain Objects, but
In my django app,I need to create a mapping between a username and a
If I want to do bi-directional mapping, do I need to create two mapping?
I'm trying to generate NHibernate mappings with ActiveRecord. I can create classes from a
I need to create a connection with a table that isn't in my control
If i create a new Parent object and then add new child object to
I've been doing a lot of reading about DTO's recently, having never used them
I am currently trying to create two simple one-to-one mappings using Hibernate but somehow
If I create a DTo using projection and an import mapping, is the object

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.