-->
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.  [ 2 posts ] 
Author Message
 Post subject: pb with one to many
PostPosted: Wed Feb 23, 2005 7:31 am 
Newbie

Joined: Tue Feb 22, 2005 5:27 am
Posts: 10
Hibernate version: 2.1.8


Hi,

I'm having trouble in trying to reproduce the documentation exemple "weblog"
pdf documentation chapter 17

In the finder listBlogsAndRecentItems()

when I try to display the BlogItem, I got all of them, not the one I selected
in the query ("where blogItem.datetime > :minDate")

There are 2 sql_request that I'll display in few minutes :

but looks like this :

select Blog where BlogItem.dateTime> ?

and
select BlogItem where Blog.id=?

wich gets me back all the blogItem, not those selected in the first request.

How can I tell the second request not to be execute ??
Thank you in advance.


Top
 Profile  
 
 Post subject: Re: pb with one to many
PostPosted: Wed Feb 23, 2005 9:18 am 
Newbie

Joined: Tue Feb 22, 2005 5:27 am
Posts: 10
Hibernate version: 2.1.8


well I change the content of my first post to try to be more understandable.
The problem is quite easy :

Assume I have TAB_A with one to many relation to TAB_B

TAB_A

ID INT PK
NAME CHAR(5)

TAB_B

LANG CHAR(2) PK1
TAB_A_ID INT PK2
DESC CHAR(5)

mapped whith TabA and TabB Java class

In my database I got the folowing data :

------------------------
TAB_A
ID | NAME
------------------------
1 | TOTO

------------------------------
TAB_B
LANG | TAB_A_ID | DESC
-----------------------------
FR | 1 | RAS
EN | 1 | RAS



My request is : give me all the TabA objects that match ID=1 and TabB.LANG='FR'
So the query seems to be :

Code:
Query query = session.createQuery("select tabA from TabA as tabA inner join tabA.tabBs as tabB where tabA.id =:code and tabB.lang like :lang");

List result = query.list();
for (int i=0;i<result.length();i++){

   System.out.print(((TabA)result.get(i)).getId()+" -> "+((TabA)result.get(i)).getTabBs().size());
}


output : 1 -> 2 instead of : 1 -> 1

(there is only one row with LANG='FR')

And show_sql = true displays 2 requests :

Hibernate: select TAB_A.ID, TAB_A.NAME from TAB_A taba_, TAB_B tabb_ where taba_.ID=tabb_.TAB_A_ID and ((taba_.ID=? )and(tabb_.LANG = ? ))
Hibernate: select * from TAB_B tabb_ where tabb_.TAB_A_ID=?

I think it's the second sql request that should not be asked because all of the tabB items should be in the 1st request

It's like the example in the documentation reference chapter 17 : Weblog, the finder listBlogsAndRecentItems() returns all the blog but
only the blogItems that have recentItems.

A difference I got why my program is that I don't close the session otherwise I got :
net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection - no session or session was closed

I guess there's something wrong I did not understand about Hibernate but it's hard when you begin !
Thanks a lot for any help.

[/code]


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