-->
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.  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: one-to-many in one select
PostPosted: Mon Nov 24, 2003 3:25 pm 
Newbie

Joined: Mon Nov 24, 2003 2:38 pm
Posts: 11
Location: Santa Catarina, Brazil
Hello,

Can I change the example below to make Hibernate load this objet in one sql statement?

Thanks!

Mappings:

<?xml version='1.0' encoding='windows-1252'?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="Projeto" table="SAC.ESACPROJETO">
<composite-id name="projetoPK" class="ProjetoPK">
<key-property name="cdProjeto" type="int"/>
</composite-id>
<property name="nmProjeto" type="string"/>
<property name="sgProjeto" type="string"/>
<property name="deBasevss" type="string"/>
<bag name="sistemas">
<key column ="cdProjeto" />
<one-to-many class="Sistema"/>
</bag>
</class>
</hibernate-mapping>

<?xml version='1.0' encoding='windows-1252'?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="Sistema" table="SAC.ESACSISTEMA">
<composite-id name="sistemaPK" class="SistemaPK">
<key-property name="cdProjeto" type="int"/>
<key-property name="cdSistema" type="int" />
</composite-id>
<property name="nmSistema" type="string"/>
<property name="sgSistema" type="string"/>
<property name="deHomedigital" type="string"/>
</class>
</hibernate-mapping>

Find:

session.find("from Projeto p where p.projetoPK.cdProjeto=1");

Generated sqls by Hibernate:

Hibernate: select projeto0_.cdProjeto as cdProjeto, projeto0_.nmProjeto as nmProjeto, projeto0_.sgProjeto as sgProjeto, projeto0_.deBasevss as deBasevss from SAC.ESACPROJETO projeto0_ where (projeto0_.cdProjeto=1 )

Hibernate: select esacsist0_.cdProjeto as cdProjeto__, esacsist0_.cdSistema as cdSistema__, esacsist0_.cdProjeto as cdProjeto, esacsist0_.cdSistema as cdSistema, esacsist0_.nmSistema as nmSistema, esacsist0_.sgSistema as sgSistema, esacsist0_.deHomedigital as deHomedi5_ from SAC.ESACSISTEMA esacsist0_ where esacsist0_.cdProjeto=?

Hibernate: select sistema0_.cdProjeto as cdProjeto, sistema0_.cdSistema as cdSistema, sistema0_.nmSistema as nmSistema, sistema0_.sgSistema as sgSistema, sistema0_.deHomedigital as deHomedi5_ from SAC.ESACSISTEMA sistema0_ where sistema0_.cdProjeto=? and sistema0_.cdSistema=?

Hibernate: select sistema0_.cdProjeto as cdProjeto, sistema0_.cdSistema as cdSistema, sistema0_.nmSistema as nmSistema, sistema0_.sgSistema as sgSistema, sistema0_.deHomedigital as deHomedi5_ from SAC.ESACSISTEMA sistema0_ where sistema0_.cdProjeto=? and sistema0_.cdSistema=?

Hibernate: select sistema0_.cdProjeto as cdProjeto, sistema0_.cdSistema as cdSistema, sistema0_.nmSistema as nmSistema, sistema0_.sgSistema as sgSistema, sistema0_.deHomedigital as deHomedi5_ from SAC.ESACSISTEMA sistema0_ where sistema0_.cdProjeto=? and sistema0_.cdSistema=?

Hibernate: select sistema0_.cdProjeto as cdProjeto, sistema0_.cdSistema as cdSistema, sistema0_.nmSistema as nmSistema, sistema0_.sgSistema as sgSistema, sistema0_.deHomedigital as deHomedi5_ from SAC.ESACSISTEMA sistema0_ where sistema0_.cdProjeto=? and sistema0_.cdSistema=?

Hibernate: select sistema0_.cdProjeto as cdProjeto, sistema0_.cdSistema as cdSistema, sistema0_.nmSistema as nmSistema, sistema0_.sgSistema as sgSistema, sistema0_.deHomedigital as deHomedi5_ from SAC.ESACSISTEMA sistema0_ where sistema0_.cdProjeto=? and sistema0_.cdSistema=?

Hibernate: select sistema0_.cdProjeto as cdProjeto, sistema0_.cdSistema as cdSistema, sistema0_.nmSistema as nmSistema, sistema0_.sgSistema as sgSistema, sistema0_.deHomedigital as deHomedi5_ from SAC.ESACSISTEMA sistema0_ where sistema0_.cdProjeto=? and sistema0_.cdSistema=?

Hibernate: select sistema0_.cdProjeto as cdProjeto, sistema0_.cdSistema as cdSistema, sistema0_.nmSistema as nmSistema, sistema0_.sgSistema as sgSistema, sistema0_.deHomedigital as deHomedi5_ from SAC.ESACSISTEMA sistema0_ where sistema0_.cdProjeto=? and sistema0_.cdSistema=?

Hibernate: select sistema0_.cdProjeto as cdProjeto, sistema0_.cdSistema as cdSistema, sistema0_.nmSistema as nmSistema, sistema0_.sgSistema as sgSistema, sistema0_.deHomedigital as deHomedi5_ from SAC.ESACSISTEMA sistema0_ where sistema0_.cdProjeto=? and sistema0_.cdSistema=?

Result:

(Projeto:1;PD;Sistemas:[(Sistema:1;1;SAC), (Sistema:1;2;SP4/SIA), (Sistema:1;3;SPW), (Sistema:1;4;Fon


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 24, 2003 4:54 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
set hibernate.use_outer_join to true

Code:
Projeto p = (Projeto) session.load(Projeto.class, myProjetoPK);

will do the job,a nd this one
Code:
List l = session.find("from Projeto p left outer join fetch p.sistemas where p.projetoPK.cdProjeto=1");

too

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 25, 2003 7:43 am 
Newbie

Joined: Mon Nov 24, 2003 2:38 pm
Posts: 11
Location: Santa Catarina, Brazil
Thanks for reply.

It


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 25, 2003 8:11 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
I debugged the Hibernate and it tries to get the value of the field "Projeto__" that does not exists in the ResultSet.


I don't believe you. I think it tries to look for a field called cdProtejo__



Perhaps you JDBC driver is unable to handle having the same column aliased to two different aliases. Since it looks like you are using an unsupported driver, this would not surprise me.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 25, 2003 9:12 am 
Newbie

Joined: Mon Nov 24, 2003 2:38 pm
Posts: 11
Location: Santa Catarina, Brazil
Gavin,

Thanks for reply. You are right. My apology. I was using a SUN JDBC driver. I changed to DB2 driver and the query worked fine. The generated SQL was:

Code:
Hibernate: select projeto0_.cdProjeto as cdProjeto0_, sistemas1_.cdProjeto as cdProjeto1_, sistemas1_.cdSistema as cdSistema1_, projeto0_.nmProjeto as nmProjeto0_, projeto0_.sgProjeto as sgProjeto0_, projeto0_.deBasevss as deBasevss0_, sistemas1_.nmSistema as nmSistema1_, sistemas1_.sgSistema as sgSistema1_, sistemas1_.deHomedigital as deHomedi5_1_, sistemas1_.cdProjeto as cdProjeto__, sistemas1_.cdSistema as cdSistema__ from SAC.ESACPROJETO projeto0_ left outer join SAC.ESACSISTEMA sistemas1_ on projeto0_.cdProjeto=sistemas1_.cdProjeto where (projeto0_.cdProjeto=1 )

Hibernate: select sistema0_.cdProjeto as cdProjeto, sistema0_.cdSistema as cdSistema, sistema0_.nmSistema as nmSistema, sistema0_.sgSistema as sgSistema, sistema0_.deHomedigital as deHomedi5_ from SAC.ESACSISTEMA sistema0_ where sistema0_.cdProjeto=? and sistema0_.cdSistema=?

Hibernate: select sistema0_.cdProjeto as cdProjeto, sistema0_.cdSistema as cdSistema, sistema0_.nmSistema as nmSistema, sistema0_.sgSistema as sgSistema, sistema0_.deHomedigital as deHomedi5_ from SAC.ESACSISTEMA sistema0_ where sistema0_.cdProjeto=? and sistema0_.cdSistema=?

Hibernate: select sistema0_.cdProjeto as cdProjeto, sistema0_.cdSistema as cdSistema, sistema0_.nmSistema as nmSistema, sistema0_.sgSistema as sgSistema, sistema0_.deHomedigital as deHomedi5_ from SAC.ESACSISTEMA sistema0_ where sistema0_.cdProjeto=? and sistema0_.cdSistema=?

Hibernate: select sistema0_.cdProjeto as cdProjeto, sistema0_.cdSistema as cdSistema, sistema0_.nmSistema as nmSistema, sistema0_.sgSistema as sgSistema, sistema0_.deHomedigital as deHomedi5_ from SAC.ESACSISTEMA sistema0_ where sistema0_.cdProjeto=? and sistema0_.cdSistema=?

Hibernate: select sistema0_.cdProjeto as cdProjeto, sistema0_.cdSistema as cdSistema, sistema0_.nmSistema as nmSistema, sistema0_.sgSistema as sgSistema, sistema0_.deHomedigital as deHomedi5_ from SAC.ESACSISTEMA sistema0_ where sistema0_.cdProjeto=? and sistema0_.cdSistema=?

Hibernate: select sistema0_.cdProjeto as cdProjeto, sistema0_.cdSistema as cdSistema, sistema0_.nmSistema as nmSistema, sistema0_.sgSistema as sgSistema, sistema0_.deHomedigital as deHomedi5_ from SAC.ESACSISTEMA sistema0_ where sistema0_.cdProjeto=? and sistema0_.cdSistema=?

Hibernate: select sistema0_.cdProjeto as cdProjeto, sistema0_.cdSistema as cdSistema, sistema0_.nmSistema as nmSistema, sistema0_.sgSistema as sgSistema, sistema0_.deHomedigital as deHomedi5_ from SAC.ESACSISTEMA sistema0_ where sistema0_.cdProjeto=? and sistema0_.cdSistema=?

Hibernate: select sistema0_.cdProjeto as cdProjeto, sistema0_.cdSistema as cdSistema, sistema0_.nmSistema as nmSistema, sistema0_.sgSistema as sgSistema, sistema0_.deHomedigital as deHomedi5_ from SAC.ESACSISTEMA sistema0_ where sistema0_.cdProjeto=? and sistema0_.cdSistema=?


But Hibernate still was using more than one SQL to make this job. Beside this the result is not what I was expecting. It


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 25, 2003 9:23 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
So exactly which association is not being outerjoined?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 25, 2003 9:39 am 
Newbie

Joined: Mon Nov 24, 2003 2:38 pm
Posts: 11
Location: Santa Catarina, Brazil
The highlighted association. I would like to load the "Projeto" with "cdProjeto=1" and all "Sistemas" associated with this "Projeto".

<?xml version='1.0' encoding='windows-1252'?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="Projeto" table="SAC.ESACPROJETO">
<composite-id name="projetoPK" class="ProjetoPK">
<key-property name="cdProjeto" type="int"/>
</composite-id>
<property name="nmProjeto" type="string"/>
<property name="sgProjeto" type="string"/>
<property name="deBasevss" type="string"/>
<bag name="sistemas">
<key column ="cdProjeto" />
<one-to-many class="Sistema"/>
</bag>
</class>
</hibernate-mapping>

<?xml version='1.0' encoding='windows-1252'?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="Sistema" table="SAC.ESACSISTEMA">
<composite-id name="sistemaPK" class="SistemaPK">
<key-property name="cdProjeto" type="int"/>
<key-property name="cdSistema" type="int" />
</composite-id>
<property name="nmSistema" type="string"/>
<property name="sgSistema" type="string"/>
<property name="deHomedigital" type="string"/>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 25, 2003 10:20 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Just like Emmanuel said:

Code:
from Projeto p
left join fetch p.sistemas
where p.projetoPK.cdProjeto=1


Will fetch a Projecto and all its Sistemas in a single SQL select.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 25, 2003 12:02 pm 
Newbie

Joined: Mon Nov 24, 2003 2:38 pm
Posts: 11
Location: Santa Catarina, Brazil
I just do this and Hibernate (2.0.3) generated the folow SQLs:

Code:
Hibernate: select projeto0_.cdProjeto as cdProjeto0_, sistemas1_.cdProjeto as cdProjeto1_, sistemas1_.cdSistema as cdSistema1_, projeto0_.nmProjeto as nmProjeto0_, projeto0_.sgProjeto as sgProjeto0_, projeto0_.deBasevss as deBasevss0_, sistemas1_.nmSistema as nmSistema1_, sistemas1_.sgSistema as sgSistema1_, sistemas1_.deHomedigital as deHomedi5_1_, sistemas1_.cdProjeto as cdProjeto__, sistemas1_.cdSistema as cdSistema__ from SAC.ESACPROJETO projeto0_ left outer join SAC.ESACSISTEMA sistemas1_ on projeto0_.cdProjeto=sistemas1_.cdProjeto where (projeto0_.cdProjeto=1 )

Hibernate: select sistema0_.cdProjeto as cdProjeto, sistema0_.cdSistema as cdSistema, sistema0_.nmSistema as nmSistema, sistema0_.sgSistema as sgSistema, sistema0_.deHomedigital as deHomedi5_ from SAC.ESACSISTEMA sistema0_ where sistema0_.cdProjeto=? and sistema0_.cdSistema=?

Hibernate: select sistema0_.cdProjeto as cdProjeto, sistema0_.cdSistema as cdSistema, sistema0_.nmSistema as nmSistema, sistema0_.sgSistema as sgSistema, sistema0_.deHomedigital as deHomedi5_ from SAC.ESACSISTEMA sistema0_ where sistema0_.cdProjeto=? and sistema0_.cdSistema=?

Hibernate: select sistema0_.cdProjeto as cdProjeto, sistema0_.cdSistema as cdSistema, sistema0_.nmSistema as nmSistema, sistema0_.sgSistema as sgSistema, sistema0_.deHomedigital as deHomedi5_ from SAC.ESACSISTEMA sistema0_ where sistema0_.cdProjeto=? and sistema0_.cdSistema=?

Hibernate: select sistema0_.cdProjeto as cdProjeto, sistema0_.cdSistema as cdSistema, sistema0_.nmSistema as nmSistema, sistema0_.sgSistema as sgSistema, sistema0_.deHomedigital as deHomedi5_ from SAC.ESACSISTEMA sistema0_ where sistema0_.cdProjeto=? and sistema0_.cdSistema=?

Hibernate: select sistema0_.cdProjeto as cdProjeto, sistema0_.cdSistema as cdSistema, sistema0_.nmSistema as nmSistema, sistema0_.sgSistema as sgSistema, sistema0_.deHomedigital as deHomedi5_ from SAC.ESACSISTEMA sistema0_ where sistema0_.cdProjeto=? and sistema0_.cdSistema=?

Hibernate: select sistema0_.cdProjeto as cdProjeto, sistema0_.cdSistema as cdSistema, sistema0_.nmSistema as nmSistema, sistema0_.sgSistema as sgSistema, sistema0_.deHomedigital as deHomedi5_ from SAC.ESACSISTEMA sistema0_ where sistema0_.cdProjeto=? and sistema0_.cdSistema=?

Hibernate: select sistema0_.cdProjeto as cdProjeto, sistema0_.cdSistema as cdSistema, sistema0_.nmSistema as nmSistema, sistema0_.sgSistema as sgSistema, sistema0_.deHomedigital as deHomedi5_ from SAC.ESACSISTEMA sistema0_ where sistema0_.cdProjeto=? and sistema0_.cdSistema=?

Hibernate: select sistema0_.cdProjeto as cdProjeto, sistema0_.cdSistema as cdSistema, sistema0_.nmSistema as nmSistema, sistema0_.sgSistema as sgSistema, sistema0_.deHomedigital as deHomedi5_ from SAC.ESACSISTEMA sistema0_ where sistema0_.cdProjeto=? and sistema0_.cdSistema=?


And the result was eight rows:

[code]1. (Projeto:1;PD;[(Sistema:1;1;SAC)])
2. (Projeto:1;PD;[(Sistema:1;2;SP4/SIA)])
3. (Projeto:1;PD;[(Sistema:1;3;SPW)])
4. (Projeto:1;PD;[(Sistema:1;4;Fon


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 25, 2003 12:25 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
So, you are trying to tell me that Hibernate is putting each element of the collection in a different instance of Projecto???

You obviously have something very screwy somewhere.

I suggest you take one of the Hibernate examples from the doco, get that working, then try and adapt it to your usecase. 'Cos i can't quite see where your problem is. It might be anywhere from broken mapping to broken test data.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 25, 2003 12:51 pm 
Newbie

Joined: Mon Nov 24, 2003 2:38 pm
Posts: 11
Location: Santa Catarina, Brazil
Gavin,

I will follow your suggestion.

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2003 9:42 am 
Newbie

Joined: Mon Nov 24, 2003 2:38 pm
Posts: 11
Location: Santa Catarina, Brazil
Hello,

I changed my mappings to don


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2003 9:47 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
This is expected. We don't distinctify the result set. You can easily do it yourself by using

Code:
Collection results = new HashSet( query.list() );



or, in this case, simply by calling query.uniqueResult().


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2003 10:23 am 
Newbie

Joined: Mon Nov 24, 2003 2:38 pm
Posts: 11
Location: Santa Catarina, Brazil
Gavin,

It worked fine.

Why it didn't work as expected using "composite-id"?

Does Hibernate have any limitations using "composite-id"?

The example using "composite-id" shouldn't work exactly like the one using "id"?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2003 10:27 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
No. Hibernate has no problems.

I think you forgot to implement equals()/hashCode() on your composite-id class, as is clearly required by the documentation.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 16 posts ]  Go to page 1, 2  Next

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.