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]