-->
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: Hooks on caching events ?
PostPosted: Mon Feb 21, 2005 9:51 am 
Beginner
Beginner

Joined: Thu Feb 03, 2005 12:48 pm
Posts: 22
Hi,

When I navigate through my persistent objects, usually most are already in cache.
When I am requesting one that is not in cache, I would like to be notified and be able to define the HQL query responsible to retrieve the requested object and others to prefetch.

The background is that I am using huge timeseries tables (multi Gb), I want to access them by simple object navigation and I want to be able to fine tune the prefetching (the queries that must be used for prefetching).

Can I achieve this with Hibernate, and how ?

Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 21, 2005 9:57 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I'm inclined to give you this ....

.... but .... have you seen the <loader> tag in HB3?

This lets you define a query (HQL or SQL) that will be called whenever Hibernate wants to fetch an object. ( Of course, it is also used for get() and load() ).

Does that solve yr problem?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 21, 2005 9:58 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
The other option is to just provide a tiny little overrideable method in DefaultLoadEventListener.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 21, 2005 11:38 am 
Beginner
Beginner

Joined: Thu Feb 03, 2005 12:48 pm
Posts: 22
Thanks Gavin.

1- I have seen in the hb3 doc, "16.5. Custom SQL for loading": looks great.
When using this feature, if the HQL query returns more than the requested row, will the additional objects be instantiated and loaded into the cache, really available for later object navigation ?


2- But I am not so far, I am new to Hibernate.

I have a class Security, having a price history.
It looks something like :
Code:
        class Security {
           Map<Date,Double> price;
        }

I am currently struggling with the documenation. On chap 6.2 the <map> tag is explained, but for example for "access" it just says it changes the "strategy". How to understand what really happens when I use a given option of the tag ?

I have achieved mapping the Map in the following way :

Code:
    <class
       name="hibdemo.Security"
       table="security">

        <id
         name="securityId"
         type="int"           
         column="security_id">
         <generator class="assigned"/>
      </id>


      <map name="prices"
         sort="natural"
         lazy="true"
         table="security_prices"
         >
               
          <key column="security_id"/>
          <index
             column="date"
             type="hibdemo.BusinessDateType"
          />
          <element column="price" type="double"/>
      </map>
    </class>


It works, but the problem is that as soon I read a price from the Map, it actually reads all the prices for the holding Security, which is too much (huge table).
How to avoid this ?
Am I not looking at right place of the documentation ?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 21, 2005 10:58 pm 
Beginner
Beginner

Joined: Sat Jan 22, 2005 9:11 am
Posts: 35
Location: London
Have you looked at collection filters? They can prevent the initialization of an entire collection.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 22, 2005 5:33 am 
Beginner
Beginner

Joined: Thu Feb 03, 2005 12:48 pm
Posts: 22
beniji wrote:
Have you looked at collection filters? They can prevent the initialization of an entire collection.


I want the business object to expose a Map, this is the requirement.

I just would like to have a SortedMap, whose key is a date and value is a price.

When I call map.get(today), and the price is not already in cache, it should call "16.5. Custom SQL for loading", and allow me to choose which prices to load.

Is that possible ? How ?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 22, 2005 7:00 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Sorry, your requirements are not at all clear.

Though, by the sounds of what you are saying, you aren;t interested in a collection mapping at all. Perhaps all you need is a custom type.

Its incredibly hard to tell.

There are many things:

(1) a collection or class <loader>
(2) a custom type
(3) a collection filter
(4) a Hibernate3 filter
(5) a custom event listener
(6) a custom UserCollectionType

I have no way of understanding which one is right for you.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 23, 2005 12:35 pm 
Beginner
Beginner

Joined: Thu Feb 03, 2005 12:48 pm
Posts: 22
Thanks a lot for your reply and patience.

Please excuse me, I try to make my requirements clearer.

I have 2 tables:

Code:
create table security (
   security_id   int      /* for example 123456 */
,   name      varchar      /* for example JBoss Corp. */
,   constraint PK primary key (security_id)
)

/* this is a really huge table */
create table price (
   security_id int       /* for example 123456 */
,   date date         /* for example 1.jan.2005 */
,   price float         /* for example 1.23 */
,   constraint PK primary key (security_id, date)
,   constraint FK foreign key (security_id) references security(security_id)
)


I have two classes :

Code:
class Security {
   private int security_id; // plus getter and setter
   private String name;     // plus getter and setter
   private SortedMap prices;// mapped to the table prices.
             // we want this map, in order to avoid the business logic programmers
             // to deal with queries, they should just nuvigate through the map

   public SecurityPrice getPrice(date) {
      return (SecurityPrice) prices.get(date);
   }
}

class SecurityPrice {           // the existence of this class is not mandatory
   private date Date;      // plus getter and setter
   private Security security;   // plus getter and setter
   private price double;      // plus getter and setter
}


When the method prices.get(date) is called, if the price for the given date is not in the cache, I would like to control which HQL is executed.
Let's say, for example, the HQL should select all the instances of securityPrice (I mean for all the securities) at given date, and load them in cache.
If later we call the method get(same_date) again, on another prices instance (belonging to another Security instance), I expect the price to be loaded from the cache.

1)As far I understand, <loader> looks as the simplest and most promising solution. But:
- How will look my <map> tag (mapping the SortedMap "prices" to the table "price") ?
- How do I pass the date to the named query ?

2) CustomType : make Security mapped by a custom type : I do not see this solution. Do you mean map Security using a UserType ?

3) Collection Filter : I do not think this fits into, given that the "prices" map does not necessarily contains all the elements I want to prefetch. Right ?

4) Same as 3) right ?

5) Custom event listener : interesting.
- Am I allowed to issue an HQL query from within the LoadEventListener, or would it cause a kind of broken query nesting ?
- When I am notified by a LoadEvent, can I influence the decision of Hibernate to use a given HQL query ?
In clear, what actions am I allowed to take when processing the event ?

6) UserCollectionType : if this allows me to map any arbitrary collection (such as my own implementation of SortedMap), I like very much.
I have found UserCollectionType and PersistentCollection in the Javadoc, but it is not clear for an outsider like me to understand how to implement PersistentCollection.


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.