-->
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: Applying filter
PostPosted: Tue Feb 26, 2008 3:41 pm 
Newbie

Joined: Thu Sep 13, 2007 2:45 pm
Posts: 10
Hi,
Please take a look at Class A

class A {

@Column(name = "type")
private String type;

@OneToMany(...)
@JoinColumn(name = "foo")
private Set<B> children = new HashSet<B>();


}


I want to load children only if type belongs to certain type.

There may be many types, let's say A, B, C, D, E.

If type == 'C' then I want to load A's children B.

How to achieve this using Filter?

I did something where I was setting a filter that works with an attribute of B.

I want to write a filter that works with an attribute of A so that based on "type" I want to load A's children.

Thanks


Top
 Profile  
 
 Post subject: Re: Applying filter
PostPosted: Tue Feb 26, 2008 3:45 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
What if you subclass A and use type as a discriminator? Then you could define the association for the subclasses that have a specific type.



Farzad-


Top
 Profile  
 
 Post subject: Re: Applying filter
PostPosted: Tue Feb 26, 2008 5:19 pm 
Newbie

Joined: Thu Sep 13, 2007 2:45 pm
Posts: 10
farzad wrote:
What if you subclass A and use type as a discriminator? Then you could define the association for the subclasses that have a specific type.



Farzad-


Is this the only way? Can you please let me know if there are any alternative approaches?

I try to use a subclass as suggested by you.

Thanks,
Servlette


Top
 Profile  
 
 Post subject: Re: Applying filter
PostPosted: Tue Feb 26, 2008 5:28 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
I can't think of any other way but that does not mean there is no other way. I would also like to know if this can be handled in a less painful way.


Farzad-


Top
 Profile  
 
 Post subject: Re: Applying filter
PostPosted: Tue Feb 26, 2008 5:38 pm 
Newbie

Joined: Thu Sep 13, 2007 2:45 pm
Posts: 10
farzad wrote:
I can't think of any other way but that does not mean there is no other way. I would also like to know if this can be handled in a less painful way.


Farzad-



The scenario goes something like this:

Take a look at class A I posted before and then take a look at C below.


class C {

@ManyToOne(...)
@JoinColumn(name = "bar")
private A a;

}

I am trying to load class C that is mapped to a table.
When "C" gets loaded it loads "A" and "A" loads "B".

I want to control loading of B in A as for a particular type there are 100's of B.

I want an HQL or something thru which I want to take control over B's loading.

I am clueless as to how to address this as I am relatively a new Hibernate user.

Your experience or knowledge if you share with me will be really helpful.



Thanks


Top
 Profile  
 
 Post subject: Re: Applying filter
PostPosted: Tue Feb 26, 2008 5:40 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
The default behaviour in HQL is not to fetch relations unless a join fetch is specified so do your query in hql and you will be happy.



Farzad-


Top
 Profile  
 
 Post subject: Re: Applying filter
PostPosted: Tue Feb 26, 2008 6:05 pm 
Newbie

Joined: Thu Sep 13, 2007 2:45 pm
Posts: 10
farzad wrote:
The default behaviour in HQL is not to fetch relations unless a join fetch is specified so do your query in hql and you will be happy.



Farzad-


In my case,
the HQL goes something like this:

select * from C c where c.a.b.attr = "something";

For the sake of clarity I simpilfied the above query.

It loads C, A and all B.

During the select, I want to tell Hibernate, don't load B, if A.type = "foo";

Though B's fetch type is set to lazy in A, it loads B as I am doing something like c.a.b.attr = 'something' using joins.

Thanks


Top
 Profile  
 
 Post subject: Re: Applying filter
PostPosted: Tue Feb 26, 2008 6:07 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Code:
select c from C c where c.a.b.attr = "something


will only load Cs and nothing else. Lazy fetch instructions are not used here so it doesn't matter what you have put in your mapping.


Farzad-


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.