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]