-->
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.  [ 9 posts ] 
Author Message
 Post subject: Is Hibernate able to query associations?
PostPosted: Fri Mar 13, 2009 6:31 am 
Newbie

Joined: Thu Mar 12, 2009 3:41 pm
Posts: 9
Hi there,

I’m currently working on a project using hibernate to access our database. So far I like hibernate very much.

Now I have the following problem and hope anybody can help me on this.


I have two classes which are associated with one-to-many. My object tree looks similar like this:

ProductList
|- id
|- name
|- Products (0..*)
|- id
|- name
|- deleted


The entity ProductList has a set of Products assigned. I modeled that with hibernate 3 and the read write operations work.

Now I want the ProductList only to contain Products which are not marked deleted in the database.
As you can imagine the attribute deleted of the Product entity is a flag showing whether the Product was deleted (I don’t want the entity really to be deleted from database. I just want to set the flag).

What I would need is a possibility to implement such a sort of read mechanism.

What I want to do in words.

Read ProductList by id
Read products for this ProductList (lazy or not) where product.deleted = false


Is Hibernate able to do this? Ist there a possible query to solve that issue? Or do I Have to remove the one-to-many association and make 2 selects in my business logic an assign the products to the list by hand?

Best regards
Tom




Further Information:
Hibernate v3
HSQLDB 1.8.0


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 13, 2009 7:01 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
You can use Filters to filter the products-association in your list:
Code:

@Entity
@FilterDef(name="deletedFilter")
@Filter(name="deletedFilter", condition="deleted = false")
public class Product...

and in your List:
Code:
..
@Filter(name="deletedFilter", condition="deleted = false")
private Set<Product> products;


You have to enable Filters on your session, with session.enableFilter("deletedFilter")

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 13, 2009 8:00 am 
Newbie

Joined: Thu Mar 12, 2009 3:41 pm
Posts: 9
Ok I will hava a look into filters.
Is the filtering handled by java classes not by the database system? This could be a performance issue maybe.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 13, 2009 9:54 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
@Filter requires that the filter definition be enable on every session.

But in your case the condition is something like a static criteria, so you can use the @Where(clause="deleted=false"). But note that the clause attribute should be having SQL and not HQL.

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 16, 2009 4:11 am 
Newbie

Joined: Thu Mar 12, 2009 3:41 pm
Posts: 9
Thanks for your fast answers.

I'm currently a little bit confused.

1) I'm not sure when to use criterias and when filters?
I think if I don't want some statements turning on the filter and setting the parameter I would use a citeria.


2) Is there any possibility to create a hql statement that has the same effect? Is criteria mightier than hql?

best regards
Tom


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 16, 2009 4:26 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Quote:
1) I'm not sure when to use criterias and when filters?
I think if I don't want some statements turning on the filter and setting the parameter I would use a citeria.

By criteria if you mean "The Criteria API", then you have misunderstood. I just used the word "static criteria" to define your problem.

Basically what the @Where does is that, it will append what ever is in the clause attribute to the SQL generated for fetching the collection. The advantage over @Filter is that you dont need to turn on the filter for each session; it will get appended by default. The disadvantage is that you wont be able to specify dynamic parameter values. But that wont be a problem for you coz your condition is static (DELETED=1 or something like that).

Hope now you are clear.

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 16, 2009 5:58 am 
Newbie

Joined: Thu Mar 12, 2009 3:41 pm
Posts: 9
Thanks for your help. I found a solution.

And here we go:

1.) Criterias won't work. These seems to be the same like HQL but an programmatic api approach.
Like mentioned in another post (which I don't find anymore) the restrictions and where clauses filter the parent object (product list) not the association (products).
So as far as I see you can not solve my problem with HQL or Criterias.
I can: "filter for product lists which have products which are not deleted"
I can not: "filter the association to products to only contain not deleted products"


2.) @Where (or where attribute in association mapping like <set>) work for my problem. I can define the products set to only include not deleted products.
Disadvantages:
a) I can not decide if I want them include or not (for example admin view show the deleted - user view not)
b) I have to use sql in there


3.) Filters would work too, but you have to enabled them for each session.


I don't know if it is possible. But the possibility to define conditions for the reading of associations would be very nice.



by the way: if you try out my solution be careful with caching. The where condition or filters don't work on cached entities.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 16, 2009 6:04 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Well.. you haven't mentioned what is the solution YOU found...

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 16, 2009 6:36 am 
Newbie

Joined: Thu Mar 12, 2009 3:41 pm
Posts: 9
Oh sorry ;-)

As I mentioned I got filters and the where condition in my <set>-xml-element working.

As my business needs are that product lists never contain deleted products I use the where in the association definition. Point 2.


But if I would use deleted flags all over my entities or if I would have to dynamically switch between show deleted / show not I would choose the filter approach.


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