-->
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.  [ 4 posts ] 
Author Message
 Post subject: Default filtering for collections
PostPosted: Sat Mar 07, 2009 4:52 am 
Newbie

Joined: Fri Mar 06, 2009 9:53 am
Posts: 2
Hi!

I'm trying to develop a simple application for comparing prices of products among multiple sellers as an exercise to better understand Hibernate.

Currently I'm having some mapping related questions.

First of all, a Product knows all Prices it has. Price associates Product with Seller and includes information about the date, the actual price and most interestingly for this problem, an boolean flag indicating whether the price is the current price or not.

Now, the problem is that most of the time, we don't need to know the previous prices; only prices with is_current = true. I know I can create a filter to the collection in the mapping document, but it seems I need to exclusively enable it before the filtering actually occurs.

What I'd like is that the filter would be used by default and only in some certain cases it would be disabled. Is this possible with Hibernate?

Another approach for this would be to have separate collections containing the current prices and all prices. I kinda would like this approach more: when I need all the prices, I could say Product.getPrices() and when I need only the current prices, I could say Product.getCurrentPrices() without any special code in my data access layer. Is this possible and if yes, how I can do it?

Another issue is that both of the collections should be sorted in ascending order by the actual price. As far as I know, I can do it by simply having sorting="..." in my mapping. However, I haven't been able to get so far yet, so I'm asking if the Set instance set by Hibernate is of type SortedSet: that way I could easily get the cheapest and most expensive prices without going through each price in the set.

Please let me know if some things needs clarifying. The Hibernate version I'm using is 3.3.1 GA.

Best regards,
Mikko Nylén


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 07, 2009 3:22 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
Another approach for this would be to have separate collections containing the current prices and all prices. I kinda would like this approach more: when I need all the prices, I could say Product.getPrices() and when I need only the current prices, I could say Product.getCurrentPrices() without any special code in my data access layer. Is this possible and if yes, how I can do it?


It should be possible. All collection mappings support a "where" attribute that can be used as a "static" filter. Note that this is not something that can be enabled/disabled programmatically, but I guess that would suit you perfectly in this case.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 07, 2009 4:35 pm 
Newbie

Joined: Fri Mar 06, 2009 9:53 am
Posts: 2
nordborg wrote:
Quote:
Another approach for this would be to have separate collections containing the current prices and all prices. I kinda would like this approach more: when I need all the prices, I could say Product.getPrices() and when I need only the current prices, I could say Product.getCurrentPrices() without any special code in my data access layer. Is this possible and if yes, how I can do it?


It should be possible. All collection mappings support a "where" attribute that can be used as a "static" filter. Note that this is not something that can be enabled/disabled programmatically, but I guess that would suit you perfectly in this case.


Thanks for your answer. If I understood correctly, I could do this, then:

Code:
<class name="Product" table="products">
  ...
  <set name="prices" order-by="price">
    <key column="product_id" />
    <one-to-many class="Price" />
  </set>

  <set name="currentPrices" order-by="price" where="is_current = true">
    <key column="product_id" />
    <one-to-many class="Price" />
  </set>
</class>


Now I could create getPrices() and getCurrentPrices() to Product class. If I'm right, this doesn't need anything special in the Price side of the association (Price has a property product, which tells the product the price is intended for)?

However, if I have something like Product.addPrice(Price) in the Product class and it adds the price to prices AND currentPrices, could it result in problems when saving the product?

Best regards,
Mikko Nylén


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 07, 2009 5:16 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
However, if I have something like Product.addPrice(Price) in the Product class and it adds the price to prices AND currentPrices, could it result in problems when saving the product?


I have no first-hand experience of this situation. But, yes, this could be problematic unless you mark one of the set:s as inverse. I guess it makes sense to make the "currentPrices" set an inverse set. Hibernate will ignore anything you do with it and will only consider changes in the "prices" set.


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