-->
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: Two Lists, same type...
PostPosted: Tue Jun 24, 2008 6:24 am 
Newbie

Joined: Tue Jun 24, 2008 5:54 am
Posts: 4
Hibernate version:3.2.6

Hi,

Ok, I'm quite new to Hibernate so please excuse me if I am missing something fundamental...

So I have a class "MarketSnapshot" which contains two lists of another class "PricePoint"... one for bid prices, one for ask prices... my mapping document looks something like this:

<class name="MarketSnapshot" table="MARKET_SNAPSHOT">
...

<list name="bidPrices" table="PRICE_POINT" cascade="all" access="field">
<key column="SNAPSHOT_ID"/>
<index column="BID_IDX" type="int"/>
<one-to-many class="model.PricePoint"/>
</list>

<list name="askPrices" table="PRICE_POINT" cascade="all" access="field">
<key column="SNAPSHOT_ID"/>
<index column="ASK_IDX" type="int"/>
<one-to-many class="model.PricePoint"/>
</list>

...
</class>


When I persist a snapshot, I end up with something like this in my database:

id | price | amt | bid_idx | ask_idx
----+-----------------------+-------+---------+-------
3 | 16.7908338624129 | 10 | 0 |
4 | 16.8908338624129 | 10 | 1 |
5 | 17.0908338624129 | 10 | 2 |
6 | 17.2908338624129 | 10 | | 0
7 | 17.3908338624129 | 10 | | 1
8 | 17.5908338624129 | 10 | | 2

However, when I try and load the snapshot (actually when I access the bidPrices), I get:

org.hibernate.HibernateException: null index column for collection: model.MarketSnapshot.bidPrices

...surely, the null values in the ask_idx column are irrelavent when loading the bids and vica-versa.

Looking through the docs, I can't see a way to do this without having seperate tables and classes for each price type (i.e BidPrice & AskPrice)... have I missed something here?

Any help, greatly appreciated.

Regards,
Mark


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 25, 2008 5:10 pm 
Newbie

Joined: Fri Jul 29, 2005 6:21 pm
Posts: 2
two lists, same type? sounds like a certain internet meme...

Anyway, I'm having this problem myself. I have more or less the same setup at work. A single table which maps to two distinct classes. Also, there were two index columns to work with - depending on which object was inserted.

I don't see a way to get around it with the current table. I see the same HibernateException when I try to delete the records. For one, I think maybe there should have been an error on insertion. Naturally the way to do that would be to give the bid_idx and ask_idx columns a NOT NULL constraint. But then any PrincePoints inserted will fail because their BID_IDX would be null. So, I set the default value to "0" instead of NULL, which already looks ugly because 0 should be the 0th index of an array. But, to help me around that I set the "base" attribute of the list-index to 1. I figured that would solve things - but, now I get a different exception, an IndexOutOfBounds exception when it tries to access the element at index -1. I don't know enough about the internal workings of Hibernate to know exactly what the problem is, but I suspect Hibernate is getting confused by the 0 in the column and thinking it's supposed to be an index.

What I did do instead was eliminate the two index columns and have just one column instead. It works, and to me it makes more sense anyway (two columns that have the same exact information seems like a waste to me), but since I'm working with a lot of legacy code I'm not sure how well my decision is going to go over.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 26, 2008 2:26 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
What about using the "where" attribute on the <list> mappings?

Eg. something like:
Code:
<list name="bidPrices" where="not BID_IDX is null" ...>[/code>]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 26, 2008 3:54 am 
Newbie

Joined: Tue Jun 24, 2008 5:54 am
Posts: 4
yes, that works for me... didn't know you could do that. Many thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 26, 2008 3:55 am 
Newbie

Joined: Tue Jun 24, 2008 5:54 am
Posts: 4
yes, that works for me... didn't know you could do that. Many thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 26, 2008 3:55 am 
Newbie

Joined: Tue Jun 24, 2008 5:54 am
Posts: 4
yes, that works for me... didn't know you could do that. Many thanks.


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.