-->
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.  [ 2 posts ] 
Author Message
 Post subject: Restrict criteria to subclass of associated object
PostPosted: Mon Apr 02, 2007 9:22 am 
Regular
Regular

Joined: Thu Oct 19, 2006 12:07 pm
Posts: 75
Hi!


I have JBoss with Hibernate 3.2.0.cr2

Mapping :
Code:
<class name="Thing">

...

<!-- bi-directional many-to-one association to ThingGroup -->
      <many-to-one name="thingGroup"
         class="ThingGroup" not-null="true">
         <column name="THING_GROUP__KEY" not-null="true" />
      </many-to-one>
...
</class>


Class definition :

Code:
public class Thing {
...
    private ThingGroup thingGroup;
...

}


ThingGroup has three subclasses. TG1, TG2 and TG3.
It is mapped as table per class hierarchy (with a discriminator)

The question is, how do I query for Thing objects, that are associated to TG1 objects , not TG2 , TG3 or ThingGroup ?

Currently we use this code :
Code:
        Criteria critT;
        Criteria critTG;

        // Thing
        critT = getSession().createCriteria(Thing.class);

        // Thing Group
        critTG = critT.createCriteria("thingGroup");

        critT.add(isNull("someProperty"));

        critT.add(eq("someOtherProperty", someValue);

        // try to limit to TG1 type
        critTG.add(eq("discriminator", "TG1"));

The last line of course does not work, as the discriminator is not mapped to any java field.
Is there a Criterion, that would mean something like isAssignableTo(TG1.class) ?

Thanks !

David


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 24, 2007 9:13 am 
Regular
Regular

Joined: Thu Oct 19, 2006 12:07 pm
Posts: 75
I solved it using direct SQL :

Code:
critTG.add(Restrictions.sqlRestriction("{alias}.DISCRIMINATOR_COLUMN='TG1'"));


PS: Too bad I can't award myself with credits :-p


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