-->
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.  [ 7 posts ] 
Author Message
 Post subject: Restrict OneToMany Collection
PostPosted: Thu Mar 25, 2010 9:13 am 
Newbie

Joined: Thu Apr 16, 2009 7:09 am
Posts: 13
Hi,

I was wondering if it is possible to restrict a OneToMany relationship by a property on the destination table?

i.e. instead of having a single relationship that I have to cycle through to pull out the values that I want, I would like to introduce 2 OneToMany relationships, one for each type of the destination class,

so instead of
getAllChildren()

I would like to have:

getChildrenOfType1()
getChildrenOfType2()

Where I know the exact number of types that there are, so this is not an issue. For now I will write some code to cycle through the children and generate the 2 different lists that I require, but it would be nice if Hibernate could do this for me through some annotation,

Thanks,
Osian


Top
 Profile  
 
 Post subject: Re: Restrict OneToMany Collection
PostPosted: Thu Mar 25, 2010 10:09 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
It is possible to add a @Where annotation (or where attribute if you use xml mapping files) that is added as part of the SQL to fetch the collection. See http://docs.jboss.org/hibernate/stable/ ... on-enhance and http://docs.jboss.org/hibernate/stable/ ... ns-mapping


Top
 Profile  
 
 Post subject: Re: Restrict OneToMany Collection
PostPosted: Thu Mar 25, 2010 10:11 am 
Newbie

Joined: Thu Apr 16, 2009 7:09 am
Posts: 13
Ah, perfect, thanks, I thought it would be possible in some way.


Top
 Profile  
 
 Post subject: Re: Restrict OneToMany Collection
PostPosted: Mon Mar 29, 2010 10:40 am 
Newbie

Joined: Thu Apr 16, 2009 7:09 am
Posts: 13
Unfortunately, this doesn't quite meet what I want, because it doesn't use the names of the bean properties, it requires the SQL column name, and due to this, I am also concerned that it will become database specific with some of the values being passed to the query, i.e. Oracle & Hibernate treat booleans differently.

Also, I require that the returing set is restricted on a value from an association table, and not directly from the table within the SortedSet.

Any ideas? In the meantime I am going to have to do this manually, but it would be great to see Hibernate do this for me,

Thanks,
Osian


Top
 Profile  
 
 Post subject: Re: Restrict OneToMany Collection
PostPosted: Mon Mar 29, 2010 1:29 pm 
Beginner
Beginner

Joined: Wed Sep 06, 2006 12:08 pm
Posts: 48
Location: Warsaw - Poland - Europe - Milky Way
Hi Osian

I can see 2 possible answers for your question.

Answer 1
--------
Try to use filters.
Well, this solution is not based on annotation, but it is a regular Hibernate functionality. And, using filter, you can operate using property name, not database column name.
See: http://docs.jboss.org/hibernate/stable/ ... -filtering

So, in this technique, you will still get getAllChildren() Hibernate mapping, but using filters, you will never load this collection into memory. So you can have millions of children per particular entity, but using filters, Hibernate will generate database hit, every time, you use filter.

Answer 2
--------
Did you try to map your Children types as separated children type using “Inheritance mapping”?
http://docs.jboss.org/hibernate/stable/ ... nheritance

I’ve got impression that you could consider map your children as different type, using “discriminator” Hibernate technique.
Well... Then, you can try to change your mapping and give a try to the following mapping:

Code:
class Children { ... }

class ChildrenOfType1 extends Children{ ... }

class ChildrenOfType2 extends Children{ ... }



List<ChildrenOfType1> getChildrenOfType1() { ... }

List<ChildrenOfType2> getChildrenOfType2() { ... }


I don’t know if it works. I didn’t try this trick.

Regards, Adam Woźniak


Top
 Profile  
 
 Post subject: Re: Restrict OneToMany Collection
PostPosted: Mon Mar 29, 2010 1:37 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The @WhereJoinTable annotation is used to filter on the association table. Both this and @Where use SQL fragments, so they are sensitive to database dialects.

There is a method in the Session interface that help with filtering collections: Session.createQuery(Object collection, String filter). It returns a Query object. I don't know if that is useful or not for you. The example in the original post is probably better to map the allChildren collection and then split it up when getChildrenOfType1() or getChildrenOfType2() is called. It mean that only one query is needed instead of two.

An exception would be if one of the sub-collections is large but seldom accessed and the other is small but accessed very often. With large I mean several thousands of records. In this case it is probably quicker to only load the sub-collection that is needed and avoid loading lots of objects that are usually not needed.


Top
 Profile  
 
 Post subject: Re: Restrict OneToMany Collection
PostPosted: Thu Apr 01, 2010 7:09 am 
Newbie

Joined: Thu Apr 16, 2009 7:09 am
Posts: 13
I've just used local transient Lists for now due to time constraints, but I believe that the typing way would be the way to go, so hopefully I will get time to do this.

Thanks everyone for your replies.


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