-->
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: formula in many-in-one for exotic join
PostPosted: Tue Dec 19, 2006 6:05 pm 
Newbie

Joined: Tue Dec 19, 2006 5:56 pm
Posts: 2
I am new to Hibernate. I read the document without find the solution for the following scenerio.

I have two tables:
table A_main:
for_date (pk)
keyf (pk)

other columns

and A_sub:
begin_date (pk)
keyf (pk)

end_date
other columns

now I want to create an entity A which is actually a view:
select * from A_main m inner join A_sub s on m.keyf=s.keyf and m.for_date between s.begin_date and s.end_date

The sure thing is that the view to A_main is one-to-one, and table A_sub has far fewer rows than A_main.

how to write A.hbm.xml for this mapping?

My idea is along the line using the following mapping:

...

<class name="A" ...>
.... (columns in A_main)

<many-to-one name="asub" class="A_sub"> (or maybe one-to-one)
<column name="keyf"/>
<formula>
?how to write it?
</formula>
</many-to-one>

But I don't know how to write the formula to represent the "m.for_date between s.begin_date and s.end_date" query string...

Any help? Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 20, 2006 9:24 am 
Newbie

Joined: Fri Nov 17, 2006 9:16 am
Posts: 6
I am newbie, but if I understand you, you could use a named query

<query name="yourQueryName">
<![CDATA[
from A
where keyf=asub.keyf and asub.for_date between asub.begin_date and asub.end_date
]]>
</query>

To test:

Query namedQuery = new Query("yourQueryName")

List list = nameQuery.list()


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 20, 2006 9:26 am 
Newbie

Joined: Fri Nov 17, 2006 9:16 am
Posts: 6
Sorry, you must replace
asub.for_date between asub.begin_date and asub.end_date

with

for_date between asub.begin_date and asub.end_date


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 20, 2006 3:38 pm 
Newbie

Joined: Tue Dec 19, 2006 5:56 pm
Posts: 2
Thanks. Your method is one of the possible solution. But since I'd like to have a java class mapped to this entity, I have to use a hbm.xml mapping.

I kind of solving my problem by create a view with the join and map this view to a java class. Since I only need to fetch the rows, never do remove and update on it directly, it works for my purpose.


dzarco wrote:
I am newbie, but if I understand you, you could use a named query

<query name="yourQueryName">
<![CDATA[
from A
where keyf=asub.keyf and asub.for_date between asub.begin_date and asub.end_date
]]>
</query>

To test:

Query namedQuery = new Query("yourQueryName")

List list = nameQuery.list()


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.