-->
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.  [ 6 posts ] 
Author Message
 Post subject: Mapping table and need a subset of rows returned
PostPosted: Tue Nov 08, 2005 6:38 pm 
Beginner
Beginner

Joined: Fri Oct 01, 2004 7:13 am
Posts: 20
Hibernate version:2.1

Name and version of the database you are using:postgres

We have a user that has a method

Set getOrders()

which maps to the orders table

In the orders table we have a subset of those orders that completed. I would like to have another method

Set getCompletedOrders()

which would effectively return -
SELECT orders FROM orders WHERE Completed = true and userid = x;

What is the easiest way to do this? Can we specify a conditional in the mapping, or could be use a view, or should we do this with a separate bit of HQL?

Any suggestions?
Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 08, 2005 7:17 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You should use either HQL with that query, or return a new java collection backed by the existing collection (the one returned by getOrders). The first option is good if you expect the results to change frequently, especially if some other app is setting orders to complete. Using a new collection backed by the existing one avoids having to go back to the database, which is often a good idea.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 09, 2005 3:46 am 
Beginner
Beginner

Joined: Fri Oct 01, 2004 7:13 am
Posts: 20
Thanks tenwit.

This is in the Users persistent class, so I assumme the recommendation is to implement get all orders there.

Do I need to relate Users to a new class CompletedOrder which is based on a query or view to then bring back the subset?

I'll begin to knock that up and see how it looks.

Thanks


Top
 Profile  
 
 Post subject: Back the collection
PostPosted: Wed Nov 09, 2005 6:25 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Well my preference would be for the backed collection, as you don't have to go back to the database for it. But if you wanted to get a new collection of just the completed orders, then you'd just use a HQL query with the appropriate "where status = completed" or whatever.

Do you understand the idea of backing a collection? You basically want to store the "real" collection (the one returned from getOrders()) inside a new collection (AbstractSet) object, and have the iterator figure out what the next completed order is, instead of the usual next order.


Top
 Profile  
 
 Post subject: Mapping a class to a view
PostPosted: Wed Nov 09, 2005 6:39 pm 
Beginner
Beginner

Joined: Fri Oct 01, 2004 7:13 am
Posts: 20
Thanks.

I understand what you are saying about the backed collection, but don't like it. Reducing a list of java objects when the list could be 100's and 1000's is not very efficient, so I think we will either go for the HQL or what I prefer, if it will work is the view on to the data and let the view do the mapping and the query and that way you have the class which is clean, and you can implement any custom behaviour on the completed orders.

Have you done anything like this?

Has anyone else any examples of mapping to a view? Does it work, is it practical? It sounds good in theory!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 09, 2005 6:44 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
It deos work, though you have to use an sql-query, because if you use a normal query then hibernate will go to the table associated with the queried class, not the view. And if you're going to write a query, why not use normal HQL and put the where in there? As I understand it, it should be just as efficient as a view, as it uses PreparedStatements which are precompiled, just like views are. Though views might be indexable.. hmm, where did I put my SQL For Dummies book...


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