-->
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.  [ 9 posts ] 
Author Message
 Post subject: one-to-one versus multiple queries
PostPosted: Fri May 06, 2005 1:13 pm 
Newbie

Joined: Wed Apr 27, 2005 9:53 am
Posts: 10
Hi

I have a problem using one-to-one in entities

For example, i have an entity Activite who have 2 one-to-one relations like this

<dynamic-component name="locales">
<one-to-one name="en" class="ca.qc.gouv.srcq.db.entity.ActiviteEN" lazy="false"/>
<one-to-one name="fr" class="ca.qc.gouv.srcq.db.entity.ActiviteFR" lazy="false"/>
</dynamic-component>

When i get an Activite object with this code

(Activite)HibernateUtils.currentSession().createQuery("from Activite where activiteId = "+activiteId).uniqueResult()

This always do 3 queries, one for the Activite, and 2 for the 2 others... It should be doing only one since joins can be used. What i don't understand is that with some entities definition this working, while some others not working, Any clue ?


Hibernate version: 3.0

Mapping documents:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

<class name="ca.qc.gouv.srcq.db.entity.Section" table="Section">
<id name="sectionId">
<generator class="identity"/>
</id>
<property name="nom"/>
<set name="menus" order-by="ordre" lazy="true">
<key column="sectionId"/>
<one-to-many class="ca.qc.gouv.srcq.db.entity.Menu"/>
</set>
<set name="activeMenus" order-by="ordre" lazy="true" where="actif=1">
<key column="sectionId"/>
<one-to-many class="ca.qc.gouv.srcq.db.entity.Menu"/>
</set>
<set name="branches" order-by="ordre" lazy="true" table="branche">
<key column="sectionId"/>
<one-to-many class="ca.qc.gouv.srcq.db.entity.Branche"/>
</set>
</class>

<class name="ca.qc.gouv.srcq.db.entity.Menu" table="Menu">
<id name="menuId">
<generator class="identity"/>
</id>
<property name="ordre"/>
<property name="sectionId"/>
<property name="brancheId"/>
<property name="actif"/>
<property name="type"/>
<many-to-one name="section" class="ca.qc.gouv.srcq.db.entity.Section" column="sectionId" insert="false" update="false"/>
<many-to-one name="branche" class="ca.qc.gouv.srcq.db.entity.Branche" column="brancheId" insert="false" update="false"/>
<one-to-one name="contenant" class="ca.qc.gouv.srcq.db.entity.Contenant" property-ref="menuId"/>
<dynamic-component name="locales">
<one-to-one name="en" class="ca.qc.gouv.srcq.db.entity.MenuEN"/>
<one-to-one name="fr" class="ca.qc.gouv.srcq.db.entity.MenuFR"/>
</dynamic-component>
</class>

<class name="ca.qc.gouv.srcq.db.entity.MenuFR" table="menu_fr">
<id name="menuId">
<generator class="assigned"/>
</id>
<property name="nom"/>
<property name="url"/>
</class>

<class name="ca.qc.gouv.srcq.db.entity.MenuEN" table="menu_en">
<id name="menuId">
<generator class="assigned"/>
</id>
<property name="nom"/>
<property name="url"/>
</class>

<class name="ca.qc.gouv.srcq.db.entity.Branche" table="Branche">
<id name="brancheId">
<generator class="identity"/>
</id>
<property name="ordre"/>
<property name="sectionId"/>
<property name="role"/>
<many-to-one name="section" class="ca.qc.gouv.srcq.db.entity.Section" column="sectionId" insert="false" update="false"/>
<set name="menus" order-by="ordre" lazy="true">
<key column="brancheId"/>
<one-to-many class="ca.qc.gouv.srcq.db.entity.Menu"/>
</set>
<set name="activeMenus" order-by="ordre" lazy="true" where="actif=1">
<key column="brancheId"/>
<one-to-many class="ca.qc.gouv.srcq.db.entity.Menu"/>
</set>
<dynamic-component name="locales">
<one-to-one name="en" class="ca.qc.gouv.srcq.db.entity.BrancheEN" lazy="false"/>
<one-to-one name="fr" class="ca.qc.gouv.srcq.db.entity.BrancheFR" lazy="false"/>
</dynamic-component>
</class>

<class name="ca.qc.gouv.srcq.db.entity.BrancheEN" table="branche_en">
<id name="brancheId">
<generator class="assigned"/>
</id>
<property name="nom"/>
</class>

<class name="ca.qc.gouv.srcq.db.entity.BrancheFR" table="branche_fr">
<id name="brancheId">
<generator class="assigned"/>
</id>
<property name="nom"/>
</class>

<class name="ca.qc.gouv.srcq.db.entity.Contenant" table="Contenant">
<id name="contenantId">
<generator class="identity"/>
</id>
<property name="menuId"/>
<one-to-one name="menu" class="ca.qc.gouv.srcq.db.entity.Menu"/>
<set name="contenus" order-by="dateModification desc" lazy="true">
<key column="contenantId"/>
<one-to-many class="ca.qc.gouv.srcq.db.entity.Contenu"/>
</set>
<set name="activeContenus" order-by="dateModification desc" lazy="true" where="actif=1">
<key column="contenantId"/>
<one-to-many class="ca.qc.gouv.srcq.db.entity.Contenu"/>
</set>
<set name="themes" order-by="ordre" lazy="true">
<key column="contenantId"/>
<one-to-many class="ca.qc.gouv.srcq.db.entity.Theme"/>
</set>
<dynamic-component name="locales">
<one-to-one name="en" class="ca.qc.gouv.srcq.db.entity.ContenantEN"/>
<one-to-one name="fr" class="ca.qc.gouv.srcq.db.entity.ContenantFR"/>
</dynamic-component>
</class>



<class name="ca.qc.gouv.srcq.db.entity.ContenantEN" table="contenant_en">
<id name="contenantId">
<generator class="assigned"/>
</id>
<property name="titre"/>
<property name="description"/>
</class>

<class name="ca.qc.gouv.srcq.db.entity.ContenantFR" table="contenant_fr">
<id name="contenantId">
<generator class="assigned"/>
</id>
<property name="titre"/>
<property name="description"/>
</class>

<class name="ca.qc.gouv.srcq.db.entity.Contenu" table="Contenu">
<id name="contenuId">
<generator class="identity"/>
</id>
<property name="contenantId"/>
<property name="dateCreation"/>
<property name="dateModification"/>
<property name="themeId"/>
<property name="actif"/>
<property name="image"/>
<set name="documents" table="ContenuDocument">
<key column="contenuId"/>
<many-to-many column="documentId" class="ca.qc.gouv.srcq.db.entity.Document"/>
</set>
<dynamic-component name="locales">
<one-to-one name="en" class="ca.qc.gouv.srcq.db.entity.ContenuEN"/>
<one-to-one name="fr" class="ca.qc.gouv.srcq.db.entity.ContenuFR"/>
</dynamic-component>
</class>

<class name="ca.qc.gouv.srcq.db.entity.ContenuEN" table="contenu_en">
<id name="contenuId">
<generator class="assigned"/>
</id>
<property name="titre"/>
<property name="description"/>
</class>

<class name="ca.qc.gouv.srcq.db.entity.ContenuFR" table="contenu_fr">
<id name="contenuId">
<generator class="assigned"/>
</id>
<property name="titre"/>
<property name="description"/>
</class>

<class name="ca.qc.gouv.srcq.db.entity.Theme" table="theme">
<id name="themeId">
<generator class="identity"/>
</id>
<property name="ordre"/>
<set name="contenus" order-by="dateModification desc" lazy="true">
<key column="themeId"/>
<one-to-many class="ca.qc.gouv.srcq.db.entity.Contenu"/>
</set>
<set name="activeContenus" order-by="dateModification desc" lazy="true" where="actif=1">
<key column="themeId"/>
<one-to-many class="ca.qc.gouv.srcq.db.entity.Contenu"/>
</set>
<dynamic-component name="locales">
<one-to-one name="en" class="ca.qc.gouv.srcq.db.entity.ThemeEN"/>
<one-to-one name="fr" class="ca.qc.gouv.srcq.db.entity.ThemeFR"/>
</dynamic-component>
</class>

<class name="ca.qc.gouv.srcq.db.entity.ThemeFR" table="theme_fr">
<id name="themeId">
<generator class="assigned"/>
</id>
<property name="nom"/>
</class>

<class name="ca.qc.gouv.srcq.db.entity.ThemeEN" table="theme_en">
<id name="themeId">
<generator class="assigned"/>
</id>
<property name="nom"/>
</class>

<class name="ca.qc.gouv.srcq.db.entity.Document" table="document">
<id name="documentId">
<generator class="identity"/>
</id>
<dynamic-component name="locales">
<one-to-one name="en" class="ca.qc.gouv.srcq.db.entity.DocumentEN"/>
<one-to-one name="fr" class="ca.qc.gouv.srcq.db.entity.DocumentFR"/>
</dynamic-component>
</class>

<class name="ca.qc.gouv.srcq.db.entity.DocumentEN" table="document_en">
<id name="documentId">
<generator class="assigned"/>
</id>
<property name="titre"/>
<property name="url"/>
<property name="source"/>
</class>

<class name="ca.qc.gouv.srcq.db.entity.DocumentFR" table="document_fr">
<id name="documentId">
<generator class="assigned"/>
</id>
<property name="titre"/>
<property name="url"/>
<property name="source"/>
</class>

<class name="ca.qc.gouv.srcq.db.entity.ProprieteSysteme" table="proprietesysteme">
<id name="nom">
<generator class="assigned"/>
</id>
<property name="valeur"/>
</class>

<class name="ca.qc.gouv.srcq.db.entity.Role" table="role">
<id name="role">
<generator class="assigned"/>
</id>
</class>

<class name="ca.qc.gouv.srcq.db.entity.Membre" table="membre">
<id name="membreId">
<generator class="assigned"/>
</id>
<property name="motdepasse"/>
<property name="nom"/>
<property name="prenom"/>
<property name="courriel"/>
<property name="organisation"/>
<set name="roles" table="MembreRole">
<key column="membreId"/>
<many-to-many column="role" class="ca.qc.gouv.srcq.db.entity.Role"/>
</set>
</class>

<class name="ca.qc.gouv.srcq.db.entity.ActiviteType" table="activitetype">
<id name="activiteTypeId">
<generator class="identity"/>
</id>
<property name="nom"/>
</class>

<class name="ca.qc.gouv.srcq.db.entity.Activite" table="Activite">
<id name="activiteId">
<generator class="identity"/>
</id>
<property name="activiteTypeId"/>
<property name="dateActivite"/>
<property name="actif"/>
<many-to-one name="activiteType" class="ca.qc.gouv.srcq.db.entity.ActiviteType" column="activiteTypeId" insert="false" update="false"/>
<set name="documents" table="ActiviteDocument">
<key column="activiteId"/>
<many-to-many column="documentId" class="ca.qc.gouv.srcq.db.entity.Document"/>
</set>
<dynamic-component name="locales">
<one-to-one name="en" class="ca.qc.gouv.srcq.db.entity.ActiviteEN" lazy="false"/>
<one-to-one name="fr" class="ca.qc.gouv.srcq.db.entity.ActiviteFR" lazy="false"/>
</dynamic-component>
</class>

<class name="ca.qc.gouv.srcq.db.entity.ActiviteEN" table="activite_en">
<id name="activiteId">
<generator class="assigned"/>
</id>
<property name="titre"/>
<property name="description"/>
</class>

<class name="ca.qc.gouv.srcq.db.entity.ActiviteFR" table="activite_fr">
<id name="activiteId">
<generator class="assigned"/>
</id>
<property name="titre"/>
<property name="description"/>
</class>

</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
(Activite)HibernateUtils.currentSession().createQuery("from Activite where activiteId = "+activiteId).uniqueResult()

Full stack trace of any exception that occurs:

Name and version of the database you are using: mysql 4.0

The generated SQL (show_sql=true):
Hibernate: select activite0_.activiteId as activiteId, activite0_.activiteTypeId as activite2_25_, activite0_.dateActivite as dateActi3_25_, activite0_.actif as actif25_ from Activite activite0_ where (activite0_.activiteId=1)
Hibernate: select activiteen0_.activiteId as activiteId0_, activiteen0_.titre as titre27_0_, activiteen0_.description as descript3_27_0_ from activite_en activiteen0_ where activiteen0_.activiteId=?
Hibernate: select activitefr0_.activiteId as activiteId0_, activitefr0_.titre as titre28_0_, activitefr0_.description as descript3_28_0_ from activite_fr activitefr0_ where activitefr0_.activiteId=?


Debug level Hibernate log excerpt:
error lvl with log4j


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 06, 2005 2:19 pm 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
Have you tried forcing outer-join="true"?

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 07, 2005 10:12 am 
Newbie

Joined: Wed Apr 27, 2005 9:53 am
Posts: 10
I'm gonna try it

Antoine


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 08, 2005 5:32 pm 
Newbie

Joined: Wed Apr 27, 2005 9:53 am
Posts: 10
This is still not working

tried with outer-join="true" and lazy="false" without success

<class name="ca.qc.gouv.srcq.db.entity.Activite" table="Activite">
<id name="activiteId">
<generator class="identity"/>
</id>
<property name="activiteTypeId"/>
<property name="dateActivite"/>
<property name="actif"/>
<dynamic-component name="locales">
<one-to-one name="en" class="ca.qc.gouv.srcq.db.entity.ActiviteEN" outer-join="true"/>
<one-to-one name="fr" class="ca.qc.gouv.srcq.db.entity.ActiviteFR" outer-join="true"/>
</dynamic-component>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 08, 2005 5:41 pm 
Newbie

Joined: Wed Apr 27, 2005 9:53 am
Posts: 10
and here i can see with my unit test the 2 extra queries

.Hibernate: select activite0_.activiteId as activiteId, activite0_.activiteTypeId as activite2_25_, activite0_.dateActivite as dateActi3_25_, activite0_.actif as actif25_ from Activite activite0_ where (activite0_.activiteId=1)
Hibernate: select activiteen0_.activiteId as activiteId0_, activiteen0_.titre as titre27_0_, activiteen0_.description as descript3_27_0_ from activite_en activiteen0_ where activiteen0_.activiteId=?
Hibernate: select activitefr0_.activiteId as activiteId0_, activitefr0_.titre as titre28_0_, activitefr0_.description as descript3_28_0_ from activite_fr activitefr0_ where activitefr0_.activiteId=?


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 08, 2005 5:47 pm 
Newbie

Joined: Wed Apr 27, 2005 9:53 am
Posts: 10
sorry, we cant edit on these boards

if it can help to the understanding im still using this same line of code to retrieve the entity

return (ActiviteType)HibernateUtils.currentSession().createQuery("from ActiviteType where activiteTypeId = "+activiteTypeId).uniqueResult();


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 09, 2005 10:28 am 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
Could you post your hibernate.cfg.xml.

Are you using the following property: max_fetch_depth with a value of 0?
Or, are you using the deprecated use_outer_join = true?

Try to set max_fetch_depth to 1 or 2. I believe this should trigger the user of outer-join.

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 09, 2005 4:21 pm 
Newbie

Joined: Wed Apr 27, 2005 9:53 am
Posts: 10
tried with the max depth param, no changes

here is my cfg file

<hibernate-configuration>
<session-factory>
<property name="connection.datasource">java:comp/env/datasource</property>
<property name="show_sql">true</property>
<property name="max_fetch_depth">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="Hibernate.hbm.xml"/>
</session-factory>
</hibernate-configuration>


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 10, 2005 11:11 am 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
That looks good to me. Maybe the dynamic-component does not suppot joins yet.

You should try to ask the question to a member of the hibernate team.

If you think it's a bug, I think you can open one on their jira bug tracking system. They'll tell you.

Good luck.

_________________
Vincent Giguère
J2EE Developer


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