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: How to make a query with only some properties?
PostPosted: Mon Feb 11, 2008 6:28 am 
Newbie

Joined: Mon Feb 11, 2008 4:36 am
Posts: 3
Hi,

I have a table user with 4 rows:

- id
- firstname
- name
- age

I mapped user in class User. Now I want to make a query, where I get only the firstname and lastname of the table user.

Herr the sql query:

SELECT firstname, name
FROM dbo.user

my NHibernate query:

return session.CreateSQLQuery("select u.firstname, u.name from User u", "u", typeof(User)).List();

... ends with an error. But if I make the same query with all properties, it works ...

return session.CreateSQLQuery("select u.id, u.firstname, u.name. u.age from User u", "u", typeof(User)).List();

Why it doesn't run with only some properties?

regards,
Tribjun


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 11, 2008 10:29 am 
Newbie

Joined: Mon Feb 11, 2008 4:50 am
Posts: 6
I don't usually work with CreateSQLQuery, but here's my two cents worth;

the 2 strings from the FROM clause may not be able to be projected onto a new instance of type User. You should look into having the call return an array of strings instead of a instance of User.

ohw yeah, before I forget, it would be nice to be able to read the exception details with these kinds of problems...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 11, 2008 12:22 pm 
Regular
Regular

Joined: Tue Dec 25, 2007 3:41 pm
Posts: 57
Location: Argentina
Hi,

You can make a DTO class with the 2 properties you need, and then use Transformations for convert the resultset to the "DTO class".

Here are simples examples:
http://unhaddins.googlecode.com/svn/bra ... Fixture.cs

You can checkout here to get the completes examples with the libraries:
http://unhaddins.googlecode.com/svn/bra ... es.Course/

_________________
Dario Quintana


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.