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.  [ 4 posts ] 
Author Message
 Post subject: DetachedCriteria: CreateCriteria vs CreateAlias
PostPosted: Thu Aug 30, 2007 6:13 am 
Newbie

Joined: Tue Aug 15, 2006 11:01 am
Posts: 10
NHibernate version:
1.2.0.4000
Mapping documents:
(abreviated for clarity)
Code:
<hibernate-mapping  auto-import="true" default-lazy="false" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:nhibernate-mapping-2.2">
  <class name="FRS.Business.Job, FRS.Entities" table="JOBS">
    <id name="Id" access="property" column="JOBID" type="Int32" unsaved-value="0">
      <generator class="sequence">
        <param name="sequence">JOBSPK</param>
      </generator>
    </id>
    <bag name="Vehicles" access="field.camelcase-underscore" table="VEHICLES" lazy="false">
      <key column="JOBID" />
      <one-to-many class="FRS.Business.Vehicle, FRS.Entities" />
    </bag>
  </class>
  <class name="FRS.Business.Vehicle, FRS.Entities" table="VEHICLES">
    <id name="Id" access="property" column="VEHICLEID" type="Int32" unsaved-value="0">
      <generator class="native">
        <param name="sequence">VEHICLESPK</param>
      </generator>
    </id>
    <many-to-one name="Job" access="property" class="FRS.Business.Job, FRS.Entities" column="JOBID" />
    <bag name="RunningVehicles" access="field.camelcase-underscore" table="RUNNINGVEHICLES" lazy="false">
      <key column="VEHICLEID" />
      <one-to-many class="FRS.Business.RunningVehicle, FRS.Entities" not-found="ignore" />
    </bag>
    <joined-subclass name="FRS.Business.StandByVehicle, FRS.Entities" table="STANDBYS">
      <key column="VEHICLEID" />
    </joined-subclass>
  </class>
  <class name="FRS.Business.RunningVehicle, FRS.Entities" table="RUNNINGVEHICLES">
    <id name="Id" access="property" column="RUNNINGVEHICLEID" type="Int32" unsaved-value="0">
      <generator class="sequence">
        <param name="sequence">RUNNINGVEHICLESPK</param>
      </generator>
    </id>
    <many-to-one name="Vehicle" access="field.camelcase-underscore" class="FRS.Business.Vehicle, FRS.Entities" column="VEHICLEID" />
  </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
DetachedCriteria criteria = DetachedCriteria.For(typeof(Job), "j")
                    .CreateAlias(Job.Properties.Vehicles, "v", JoinType.LeftOuterJoin)
                    .CreateAlias("v." + Vehicle.Properties.RunningVehicles, "r", JoinType.LeftOuterJoin);
//go on to use the criteria as a subquery




Full stack trace of any exception that occurs:

Code:
NHibernate.QueryException: duplicate alias: j
at NHibernate.Loader.Criteria.CriteriaQueryTranslator.CreateAliasCriteriaMap()
at NHibernate.Loader.Criteria.CriteriaQueryTranslator..ctor(ISessionFactoryImplementor factory, CriteriaImpl criteria, Type rootEntityName, String rootSQLAlias)
at NHibernate.Loader.Criteria.CriteriaLoader..ctor(IOuterJoinLoadable persister, ISessionFactoryImplementor factory, CriteriaImpl rootCriteria, Type rootEntityName, IDictionary enabledFilters)
at NHibernate.Impl.SessionImpl.Find(CriteriaImpl criteria, IList results)
at NHibernate.Impl.SessionImpl.Find[T](CriteriaImpl criteria)
at NHibernate.Impl.CriteriaImpl.List[T]()


Name and version of the database you are using:
Oracle 10g

Diagnosis

This problem seems to be limited to DetachedCriteria.

If I try and do the same thing with session.CreateCriteria, it works fine.

Also, changing the CreateAlias to CreateCriteria works fine.

However, I can't do either of these as I need to left outer join on more than one child of the Job and this is part of a sub query.

Is this a limitation or a bug?

Thanks


Top
 Profile  
 
 Post subject: Related
PostPosted: Thu Aug 30, 2007 6:25 am 
Newbie

Joined: Tue Aug 15, 2006 11:01 am
Posts: 10
Related to this:

This produces a left outer join on the RunningVehicles.
Code:
DetachedCriteria criteria = DetachedCriteria.For ( typeof ( Job ), "j" );
criteria.CreateCriteria( "j." + Job.Properties.Vehicles, "v", JoinType.LeftOuterJoin);
criteria.CreateCriteria ( "v." + Vehicle.Properties.RunningVehicles, "r", JoinType.LeftOuterJoin );



This creates an inner join on RunningVehicles.
Code:
DetachedCriteria criteria = DetachedCriteria.For ( typeof ( Job ), "j" )
            .CreateCriteria( "j." + Job.Properties.Vehicles, "v", JoinType.LeftOuterJoin);
criteria.CreateCriteria ( "v." + Vehicle.Properties.RunningVehicles, "r", JoinType.LeftOuterJoin );


Again, correct behaviour or a bug?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 30, 2007 1:51 pm 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
HAve you looked at the GetCriteriaByAlias or GetCriteriaByPath methods of the DetachedCriteria object?
That would allow you to attach your criterion to different children.

Mike

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 31, 2007 4:21 am 
Newbie

Joined: Tue Aug 15, 2006 11:01 am
Posts: 10
mnichols wrote:
HAve you looked at the GetCriteriaByAlias or GetCriteriaByPath methods of the DetachedCriteria object?
That would allow you to attach your criterion to different children.

Mike


AFAIK, they're only in the repository at the moment.


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