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: SQL query loading an entity + many to many collections
PostPosted: Thu Mar 01, 2007 10:52 am 
Newbie

Joined: Thu Jan 25, 2007 6:47 am
Posts: 2
Nevermind : http://jira.nhibernate.org/browse/NH-925
(but then couldn't aliases injection be the solution?)
--
Hi,

I'm writing my own SQL query for a very specific need where I'd like to get an entity with some of its collection preloaded with some voodo paging filtering statements not included here.

I can get one-to-one and one-to-many associations to load without problems, but I can't figure out a way to map a many-to-many association in my query...

In the following example, candidate has an 1:n relation with contact and an n:m relation with knownby through a link table (linkcandidateconsultant), which has an 1:1 relation with the consultant table.

I dutifully RTFM'd but although the docs say that custom sql queries can preload collections, I could not find any example for this.

Thanks for your help!

Hibernate version: 1.2b1

Mapping documents:
Code:
    <sql-query name="candidatesBulk">
        <return alias="v_cand" class="foo.Candidate, Foo"/>
        <return-join alias="v_contact" property="v_cand.Contacts"/>
        <return-join alias="v_knownby" property="v_cand.KnownBy" />
        SELECT {v_cand.*},{v_knownby.*}
        FROM v_candidates v_cand
LEFT OUTER JOIN Contact v_contact on and v_cand.id=v_contact.CandidateId
        LEFT OUTER JOIN LinkCandidateConsultant v_knownby on v_knownby.deleted=0 and v_knownby.ConsultantId=v_cand.Id
    </sql-query>


Code between sessionFactory.openSession() and session.close():
Code:
ArrayList rows=NHibernateSession.GetNamedQuery("candidatesBulk").List();

foreach(object[] row in rows)
{
// because I'm using outer joins I'll get many times the same leftmost entity, I can live with that.
if(!myCollection.Contains(row[0]){
  myCollection.Add(row[0]);
}
}


Full stack trace of any exception that occurs:
Code:
at NHibernate.Loader.Custom.SQLQueryParser.ResolveProperties(String aliasName, String propertyName, IDictionary fieldResults, ISqlLoadable currentPersister, String suffix) in SQLQueryParser.cs:line 236
at NHibernate.Loader.Custom.SQLQueryParser.ResolveCollectionProperties(String aliasName, String propertyName, IDictionary fieldResults, ISqlLoadable elementPersister, ISqlLoadableCollection currentPersister, String suffix, String persisterSuffix) in SQLQueryParser.cs:line 169
at NHibernate.Loader.Custom.SQLQueryParser.SubstituteBrackets() in SQLQueryParser.cs:line 107
at NHibernate.Loader.Custom.SQLQueryParser.Process() in SQLQueryParser.cs:line 52
at NHibernate.Loader.Custom.SQLCustomQuery..ctor(ISQLQueryReturn[] queryReturns, String sqlQuery, ICollection additionalQuerySpaces, ISessionFactoryImplementor factory) in SQLCustomQuery.cs:line 54
at NHibernate.Impl.SessionImpl.List(NativeSQLQuerySpecification spec, QueryParameters queryParameters, IList results) in SessionImpl.cs:line 5210
at NHibernate.Impl.SessionImpl.List(NativeSQLQuerySpecification spec, QueryParameters queryParameters) in SessionImpl.cs:line 5195
at NHibernate.Impl.SqlQueryImpl.List() in SqlQueryImpl.cs:line 124

[/b]


Top
 Profile  
 
 Post subject: Any solution for this problem?
PostPosted: Thu Jul 26, 2007 3:25 pm 
Newbie

Joined: Thu Jul 26, 2007 3:23 pm
Posts: 2
We are also facing the exact issue while mapping many-to-many relationship. Were you able to find a solution?


Top
 Profile  
 
 Post subject: Any solution for this problem?
PostPosted: Thu Jul 26, 2007 3:25 pm 
Newbie

Joined: Thu Jul 26, 2007 3:23 pm
Posts: 2
We are also facing the exact issue while mapping many-to-many relationship. Were you able to find a solution?


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.