-->
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: Inverse mapping of <any> relationship
PostPosted: Mon Jan 12, 2004 4:14 pm 
Beginner
Beginner

Joined: Thu Dec 11, 2003 11:59 am
Posts: 24
Hi,

I'm having trouble figuring out how to map the inverse of an <any> relationship. For example, here is part of the mapping for a Quota object:

Code:
...
<any name="object" id-type="int">
  <column name="object_class"/>
  <column name="object_id"/>
</any>
...


These quotas can be applied to many different types of objects. Given the quota, I can getObject and to get the object that it is applied to. This works well.

However, I can't figure out how to get the set of quotas for a given object (e.g. User object). If it was not an <any> relationship, I would do something like:

Code:
...
<set name="quotas" inverse="true">
  <key column="user_id"/>
  <one-to-many class="User"/>
</set>
...


But what do I put in the <key> to show that it refers to the "object_class" and "object_id" of my <any> relationship?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2004 4:36 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
This looks like a really broken object model to me. However, have you tried to use:

Code:
<set name="quotas" inverse="true">
  <key column="object_id"/>
  <one-to-many class="Quota"/>
</set>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2004 4:51 pm 
Beginner
Beginner

Joined: Thu Dec 11, 2003 11:59 am
Posts: 24
This syntax seems to be ok, but it doesn't do what I want. Fetching the quotas for a User returns all of the quota objects that have that are associated with an object with the same id as the user, no matter the type. e.g. if my user's id is 1, I get the quotas associated with all objects that have an id of 1.

As for the object model being broken, that is possible. It shouldn't preclude the ability to navigate <any> relationships bidirectionally. It's not like the SQL is that difficult to figure out:

Code:
select * from quota where object_class = ? and object_id = ?


In fact, I have a workaround which is doing exactly this. But, this is really the responsibility of the mapping to handle. The fact that the SQL is so straightforward leads me to believe that probably supports this, even if I can't figure out how.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2004 5:27 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You could add an additional where="object_class=xxx" attribute for the set, where xxx is the discriminator value for your User class in the table.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2004 7:04 pm 
Beginner
Beginner

Joined: Thu Dec 11, 2003 11:59 am
Posts: 24
That did the trick.

Thanks!


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.