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.  [ 5 posts ] 
Author Message
 Post subject: one outer join to much - bug or feature?
PostPosted: Fri Jun 15, 2007 10:02 am 
Newbie

Joined: Sun May 13, 2007 6:16 pm
Posts: 3
hi,

i have the following classes

class group{
+ string name
+ user createdBy
}

class user{
+ int id;
+ string name;
}

class participant{
+ int roleId
+ int groupId
}

I have the following tables:

group
user
user_group


I have the following user/participant mapping:

<class name="user" table="[user]" lazy="false">

<id name="Id" column="id" type="Int32" unsaved-value="-1">
<generator class="identity"></generator>
</id>

<property name="Name" column="Name" type="String" />

<joined-subclass name="Participant "table="user_group" >
<key column="UserId"/>
<property name="GroupId" column="GroupId" type="Int32" />
<property name="Role" column="ParticipantRole"/>
</joined-subclass>

what I don't understand and even consider a bug is. why if I load a User NHibernate creates an outer join on user_group?!

I only whant one single user. The fields requested by the left outer join to "user_group" couldn be populated anyway.

bug or feature?


Top
 Profile  
 
 Post subject: lazy=true
PostPosted: Fri Jun 15, 2007 6:47 pm 
Newbie

Joined: Sun May 13, 2007 6:16 pm
Posts: 3
changing the fetching strategy (lazy=true) helped.

But I am still not happy with the behavior, since the query is performed for a type (Participant), which was not requested.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 18, 2007 1:49 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
The "problem" is that in the joined-subclass inheritance strategy, an outer join must be performed so that NHibernate can know what the underlying type actually is. This behavior is by design. Suppose you are loading a User by id: just because you tell NH that you are looking for a User doesn't mean it isn't actually a Participant. The inheritance semantics imply that a Participant "is-a" User.


Top
 Profile  
 
 Post subject: Does not make sence to me - since the upcast would fail
PostPosted: Mon Jun 18, 2007 5:48 pm 
Newbie

Joined: Sun May 13, 2007 6:16 pm
Posts: 3
class User(){
...
}

class Participant : User(){
...
}


User user = session.Load<User>();
Participant participant = (Participant)user; //upcast fails


The upcast would fail anyway, so I don't see the point. Or do I miss sth.?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 18, 2007 10:58 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
The upcast would not necessarily fail. When NH loads an object within an inheritance hierarchy from the DB, it loads it into its proper type. If it is saved as a Participant, it is loaded thereas; likewise User. In the example you cite, obviously a User reference can reference a Participant.

So, if you were doing a load from the database of a User without any foreknowledge of the subclass, you would want to "question" the object for its type before attempting such a cast:

Code:
if ( user is Participant ) { ... }


NH has to load the object as it was saved, into it's proper class. In order to do this, it has to figure out what that is. For the joined-subclass strategy (for the time being, until mixed strategies are supported) this requires an outer join to every table in the hierarchy to determine the subclass. This is why the docs list the table-per-hierarchy strategy (which uses a discriminator) as much more performant.

I guess the question would be: if this is so unacceptable, what behavior do you imagine would be preferable?


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