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.  [ 2 posts ] 
Author Message
 Post subject: Native SQL query with concatenated joins.
PostPosted: Tue Nov 10, 2009 1:53 pm 
Newbie

Joined: Wed Sep 02, 2009 11:53 am
Posts: 5
Hi all

For a particular complicated query, I have reasons to use native SQL, HQL is no option. The examples here are much simplified.

In this simple model:

Code:
User has Projects
Project has ProjectDetails


I want to execute the following SQL script:

Code:
SELECT * FROM User usr LEFT OUTER JOIN Project pr ON pr.Owner = usr.ID LEFT OUTER JOIN ProjectDetail prd ON prd.Proj = pr.ID


To let NHibernate hydrate the entities automatically, I use

Code:
session.CreateSQLQuery(query)
    .AddEntity("usr", typeof(User))
    .AddJoin("pr", "usr.Projects")
    .AddJoin("prd", "pr.Details");


Unfortunately, the collection Project.Details is not populated by this query. Depending on some details, the collection keeps either uninitialized (it hits the DB when I access it), or it is initialized with an empty collection, although the project has some ProjectDetails.

After spending nearly two days of trying to get this to work, I just want to ask whether something like that is actually possible to do with NHibernate. If it is, I will post further details.

And, I do know that fetching in this way is possible with HQL. I have reasons to do it in plain SQL.

Thank you!
Jonas


Top
 Profile  
 
 Post subject: Re: Native SQL query with concatenated joins.
PostPosted: Mon Nov 16, 2009 8:15 am 
Newbie

Joined: Wed Sep 02, 2009 11:53 am
Posts: 5
Solved. The problem was that several of my tables had the same primary key column ('ID'), so NHibernate could not assign the IDs correctly to the entities.

IMHO NHibernate should at least warn the user if it receives several columns with the same name in a native SQL query. I am going to create a JIRA issue, if nobody objects..

bye
Jonas


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