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
|