-->
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: Outer Join
PostPosted: Wed Nov 26, 2008 4:26 am 
Newbie

Joined: Tue Nov 25, 2008 3:26 pm
Posts: 3
NHibernate 2.0.1.4000



SQL Server 2005


I am having some trouble getting the exact syntax to get the SQL that I need, I have tried searching but with not much luck. I guess I am doing something wrong but can't figure it out.

Here is the SQL that I would like to be generated:


Code:
SELECT *
FROM
ActivityWhereAbouts this_
   left outer join Location location2_ on this_.Locationid=location2_.Id
   inner join Person person3_ on this_.PersonId=person3_.Id
   inner join ACTIVITYTYPE activityty4_ on this_.ActivityTypeId=activityty4_.Id


But using these mappings:

Code:
<many-to-one name="Location" column="Locationid" not-null="true" fetch="join"/>
    <many-to-one name="Person" column="PersonId" not-null="true" fetch="join"/>
    <many-to-one name="ActivityType" column="ActivityTypeId" not-null="true" fetch="join"/>


And this code:

Code:
public override IList<T> GetAllEntities<T>()
        {
            using (ISession session = OpenSession())
            {
                return session.CreateCriteria(typeof(T)).List<T>();
            }
        }


I get this generated SQL:

Code:
SELECT *
FROM    
ActiveActivities this_
   inner join Location location2_ on this_.Locationid=location2_.Id
   inner join ActivePersons P on this_.PersonId=P.Id
   inner join ActiveActivityTypes [AT] on this_.ActivityTypeId=[AT].Id   


Which is as I would expect. In an effort to achieve my initial goal I have tried running this code:

Code:
public  IList<T> GetAllEntities33<T>()
        {                       

            var criteria1 = DetachedCriteria.For<T>()
                .CreateCriteria("Location", JoinType.LeftOuterJoin);
            var criteria2 = DetachedCriteria.For<T>()
                .CreateCriteria("Person", JoinType.InnerJoin);
            var criteria3 = DetachedCriteria.For<T>()
                .CreateCriteria("ActivityType", JoinType.InnerJoin);

            var result = OpenSession().CreateMultiCriteria()
                .Add(criteria1)
                .Add(criteria2)
                .List();
           
            IList<T> list = (IList<T>)result;

            return list;           
        }


Which has given me the following generated SQL:
Code:
SELECT *
FROM ActivityWhereAbouts this_
      left outer join Location location1_ on this_.Locationid=location1_.Id
       left outer join ActivityWhereAbouts activitywh4_ on location1_.Id=activitywh4_.Locationid
       left outer join Person person5_ on activitywh4_.PersonId=person5_.Id
       left outer join ACTIVITYTYPE activityty6_ on activitywh4_.ActivityTypeId=activityty6_.Id;

SELECT *
FROM ActivityWhereAbouts this_
      inner join Location location3_ on this_.Locationid=location3_.Id
      inner join Person person1_ on this_.PersonId=person1_.Id
      left outer join ActivityWhereAbouts activitywh5_ on person1_.Id=activitywh5_.PersonId
      left outer join ACTIVITYTYPE activityty6_ on activitywh5_.ActivityTypeId=activityty6_.Id;


So it looks like I am on the right path but not quite there yet. Can anyone shed some light on what I am doing wrong?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2008 6:07 am 
Newbie

Joined: Tue Nov 25, 2008 3:26 pm
Posts: 3
Sorry for the double post but I have done a bit more digging.

If I run this code:
Code:
public IList<T> GetAllEntities_With_ICriteria<T>()
        {
            ISession session = OpenSession();
            ICriteria criteria; IList<T> result;

            criteria = session.CreateCriteria(typeof(T));

            ICriteria subCriteria = criteria
                .CreateCriteria("Location");

            criteria.SetFetchMode("ActivityWhereAbouts.Person", FetchMode.Select);
            criteria.SetFetchMode("ActivityWhereAbouts.ActivityType", FetchMode.Select);

            result = criteria.List<T>();
            return result;
        }


I am now getting this generated SQL:
Code:
SELECT *
FROM ActivityWhereAbouts this_
      inner join Location location1_ on this_.Locationid=location1_.Id
      left outer join ActivityWhereAbouts activitywh4_ on location1_.Id=activitywh4_.Locationid
      left outer join Person person5_ on activitywh4_.PersonId=person5_.Id
      left outer join ACTIVITYTYPE activityty6_ on activitywh4_.ActivityTypeId=activityty6_.Id


Which is not what I want but seems to be closer to what I wanted.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2008 6:18 am 
Newbie

Joined: Tue Nov 25, 2008 3:26 pm
Posts: 3
I have managed to get what I want, but I don' think that I am using it the right way :(

If I use this:

Code:

IList<T> list = session.CreateSQLQuery("SELECT * FROM " +
                        "ActivityWhereAbouts this_ left outer join Location location2_ " +
                        "on this_.Locationid=location2_.Id inner join Person person3_ on " +
                        "this_.PersonId=person3_.Id inner join ACTIVITYTYPE activityty4_ on " +
                        "this_.ActivityTypeId=activityty4_.Id " +
                        "WHERE     ((this_.Inactive = 0) OR " +
                        "(this_.Inactive IS NULL)) AND " +
                        "((person3_.Inactive = 0) OR " +
                        "(person3_.Inactive IS NULL)) AND " +
                        "((activityty4_.Inactive = 0) OR " +
                        "(activityty4_.Inactive IS NULL)) AND " +
                        ":ActivitydateTime BETWEEN " +
                        "this_.StartDate AND this_.EndDate")
                        .AddEntity(typeof(T))
                        .SetDateTime("ActivitydateTime", new DateTime(2007, 03, 01))
            .List<T>();




This retrieves what I need but I wanted to be able to avoid using SQL. Is there any way that I can use something like CreateMultiCriteria to get this back without having to resort to SQL?


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.