ISQLQuery: unexpected root entity returned

48 views
Skip to first unread message

FrederikGheysels

unread,
Apr 8, 2010, 10:38:02 AM4/8/10
to nhusers
Hi all,

I'm using NHibernate 2.1.2.400, and I'm having an issue with a an
ISQLQuery query.

The reason why I use an ISQLQuery here, is that this query uses a
table for which I have no entity mapped in NHibernate.

The query looks like this:

ISQLQuery query = session.CreateSQLQuery (
"select p.*, price.* " +
"from prestation p left outer join prestationprice price on
p.PrestationId = price.PrestationId " +
"where p.Id IN ( select id from prestationregistry where ...");

'Prestationregistry' is the table that is not known by NHibernate
(unmapped, so therefore the native SQL Query).

my code continues like this:

query.AddEntity ("p", typeof(Prestation));
query.AddJoin ("price", typeof(PrestationPrice));

query.SetResultTransformer (Transformers.DistinctRootEntity);

var result = query.List();

So far so good.
I expect that I'm given a list of 'Prestation' instances as a result
of this query, since I declared 'Prestation' as being the root-object
that has to be returned by the AddEntity method.
I also expect that the PrestationPrices for each Prestation are
eagerly loaded by this query (hence the AddJoin method).

To my surprise, the List() method returns a collection of
PrestationPrice instances instead of Prestation instances.
How come ? Am I doing something wrong ? And if so, could you be so
kind to tell me what I'm doing wrong ?
Or, is this a bug ?

FrederikGheysels

unread,
Apr 8, 2010, 11:02:14 AM4/8/10
to nhusers
Additional Info:
- when I do not specify the DistinctRootEntity ResultTransformer, the
query returns instances of 'Prestation', however, multiple copies of
the same instance are present in the result.

On 8 apr, 16:38, FrederikGheysels <frederik.gheys...@pandora.be>
wrote:

nadav s

unread,
Apr 8, 2010, 4:26:50 PM4/8/10
to nhu...@googlegroups.com
few stuff:

1. why won't you map the table? falling back on native sql, IMO is worst practice when you're using a great tool that does everything for you

2. even if you decide not to map that table (if so, why?), you could use a regular criteria and just add a native sql criterion
to a regular criteria - (see Expression.Sql i belive. there you could write some_field in (Select some_field from UnmappedTable Where ...))

then you fall back on native sql only in one of the where clauses and not the whole query, but again, why won't you just map the table?

3. i'm not sure, but from the top of my head i think there's something like SetRootEntity, but i might be off



--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.


--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.

FrederikGheysels

unread,
Apr 9, 2010, 3:29:56 AM4/9/10
to nhusers
nadav, thx for the reply.

1. I do not map this table, since this table has really nothing to do
with my domain-model. The table in question is nothing more then some
kind of 'rule' table; it does not represent an entity or value object.
It just contains some primary-keys of 'Prestation' entities that
should be used in certain circumstances.

2. I will check that out. I didn't know you could use a native Sql
Criterion within a Criteria.

3. AFAIK, the 'SetRootEntity' method does not exists, but the result
that you want to have returned by the ISQLQuery is specified by the
'AddEntity' method.

> > nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com >

FrederikGheysels

unread,
Apr 9, 2010, 4:01:57 AM4/9/10
to nhusers
nadav,

I've solved it for the time being using an ICriteria with an
Expression.Sql criterion.
Thx for pointing that out. ;)

Anyway, I still wonder if the problem that I pointed out using
ISQLQuery is a bug or if I was really doing something wrong ?

On 8 apr, 22:26, nadav s <nadav...@gmail.com> wrote:

> > nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com >

Reply all
Reply to author
Forward
0 new messages