-->
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.  [ 8 posts ] 
Author Message
 Post subject: Filters and subclasses
PostPosted: Tue May 09, 2006 7:45 am 
Beginner
Beginner

Joined: Mon Aug 02, 2004 1:08 pm
Posts: 42
Is there (or will there be) any possibility to define a filter for a subclass?
Currently, subclass element does not include the filter sub-element.
Could this be implemented or is this theoretically impossible?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 09, 2006 6:54 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Is the combination of a filter on the superclass plus the subclass discriminator not fine-grained enough? What is it that you're trying to do that can't be done with those two features?

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 27, 2006 12:55 pm 
Newbie

Joined: Wed Dec 08, 2004 9:13 am
Posts: 6
tenwit wrote:
Is the combination of a filter on the superclass plus the subclass discriminator not fine-grained enough? What is it that you're trying to do that can't be done with those two features?


I have a similar issue; following is the example...

We would like to create jBPM TaskInstanceS that have an association to one of our Organization entities. As described in the jbpm docs, we have our own task factory that can create these instances (under a distinct jbpm configuration context).

We would like to select all OrganizationTaskInstances (mapping file below, with a dicriminator-value="O") with our owningOrg filter applied -- to only select TaskInstances "owned" by the organization of the currently logged in user.




Mapping file:
<hibernate-mapping>
<subclass
name="OrganizationTaskInstance"
extends="org.jbpm.taskmgmt.exe.TaskInstance"
discriminator-value="O">
<join table="Link_JbpmTaskInstances_Organizations">

<key column="taskInst_id" unique="true" />

<many-to-one name="organization"
class="Organization"
column="ORG_ID"
unique="true"
not-null="true" />
</join>
</subclass>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: filter on superclass and discriminator not enough
PostPosted: Tue Feb 06, 2007 12:34 pm 
Newbie

Joined: Tue Sep 26, 2006 7:31 am
Posts: 8
Location: Oslo
I have also experienced a couple of situations where I've really needed to put a filter on a subclass.
For instance:

Superclass: Context
A bunch uf subclasses; "Car", "Person" etc
Mapping strategy: Mix of table per class hierarchy and table per subclass (using discriminator).
Let's say that the relationship between Context and Person is 1:M, but in several contexts I'm only interested in persons of a certain type based on a "persontype" column in Person.
The discriminator helps hibernate decide what subclass to instanciate (Car, Person...) but how can I make sure that only persons of a certain type a instanciated? In the "subclass" mapping I can't use either "where" or "filter"......


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 4:51 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
According to the refdocs section 14.7, any java class or interface can be used in HQL. So assuming that you can phrase your query appropriately, you should be able to do what you need without any filters. Let's say you wanted to get all Contexts referring to Persons of type Programmer (i.e. implementations of the Person interface that also implement the Programmer interface), then you can do something like
Code:
select p from Context c, Programmer p
where c.Person.id = p.id
You can't use c.Programmer, but you can use cross-joins to fake it.

I've never tried this, so maybe you could post back with your results, I'd like to know if this works.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 07, 2007 6:15 am 
Newbie

Joined: Tue Sep 26, 2006 7:31 am
Posts: 8
Location: Oslo
Thank you for your reply tenwit!

Yes, with HQL one can do all kinds of flexible things, but we are using OpenSessionInView pattern and i want my objects to have the correct structure when they are for instance lazily loaded up while traversing the object graph. For my object graph to be correct (and my application much more efficient) I need to instanciate the correct Persons... meaning I need my object graph to be a 1:1 between Context and Person (so that Person can inherit Context) although the relationship in the db is 1:Many.

Probably there are good reasons why this does not seem possible, but it eludes me. I've tried posting in the forum about this earlier with no reply...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 07, 2007 4:40 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
So you have a discriminator on Context, with a particular column ContextType: when ContextType = PERSON, then you'll be using <subclass><join table="PERSON"> to define this Context as a Person. And in the Person table, there's a PersonType attribute, which you want to be able to filter on.

This should be possible, assuming that there's no column called PersonType on any table except Person (because we can't access Hibernate's table aliases from inside filters). Put this in your hibernate-mapping:
Code:
<filter-def name="PersonTypeFilter">
  <filter-param name="Type" type="string"/>
</filter-def>
And this in your Context class mapping:
Code:
<filter name="ByPersonType" condition="ContextType=PERSON and PersonType=:Type"/>
Note that this works only because the resulting select statement has only one column called PersonType: if it has more than one, the query will fail (ambiguous column name).

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 09, 2007 4:39 am 
Newbie

Joined: Tue Sep 26, 2006 7:31 am
Posts: 8
Location: Oslo
Thank you tenwit, that seems to work!!
Regretfully I cannot use this approach, but was rather forced to make a view in the database to be able to fill my subclasses with what they needed. The reasons for this are that I needed a couple of associations from my subclasses, and I am not able to declare them inside the <join table> inside the declared <subclass>. Only way to do this is to declare the collection outside the scope of the <join> and that brings the wrong result for me because then it can only use properties declared on the superclass. So I have to kiss lazy loading goodbye and put it in a view..... :\

Again, thank you, I was struggeling with that one!


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