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.  [ 3 posts ] 
Author Message
 Post subject: [Résolu] Jointures implicites générées trop nombreuses
PostPosted: Wed Aug 01, 2007 4:49 am 
Newbie

Joined: Wed Aug 01, 2007 4:18 am
Posts: 4
Bonjour,

Je suis plutot débutant en hibernate et mon problème est que lorsque j'utilise une requête avec des critères un peu avancés en hql, en utilisant des jointures implicites, hibernate me génère une requête avec beaucoup trop de jointures, qui sont inutiles car redondantes.


J'ai 3 tables dont les mappings, générés par hibernate tools, sont les suivants :
Code:
<class name="vtpro.model.OrgPersonnel" table="ORG_PERSONNEL">
        <id name="idPersonnel" type="integer">
            <column name="ID_PERSONNEL" precision="22" scale="0" />
            <generator class="assigned" />
        </id>
        <property name="compteSesame" type="string">
            <column name="COMPTE_SESAME" length="100" />
        </property>
        <set name="orgAffectPersonnels" inverse="true">
            <key>
                <column name="ID_PERSONNEL" precision="22" scale="0" not-null="true" />
            </key>
            <one-to-many class="vtpro.model.OrgAffectPersonnel" />
        </set>
    </class>




Code:
    <class name="vtpro.model.OrgPoste" table="ORG_POSTE">
        <id name="idPoste" type="integer">
            <column name="ID_POSTE" precision="22" scale="0" />
            <generator class="assigned" />
        </id>
        <property name="descPoste" type="string">
            <column name="DESC_POSTE" length="50" />
        </property>
        <property name="posteActif" type="string">
            <column name="POSTE_ACTIF" length="1" />
        </property>
        <property name="dateDebutValidite" type="date">
            <column name="DATE_DEBUT_VALIDITE" length="7" />
        </property>
        <property name="dateFinValidite" type="date">
            <column name="DATE_FIN_VALIDITE" length="7" />
        </property>
        <set name="orgAffectPersonnels" inverse="true">
            <key>
                <column name="ID_POSTE" precision="22" scale="0" not-null="true" />
            </key>
            <one-to-many class="vtpro.model.OrgAffectPersonnel" />
        </set>
    </class>




Code:
    <class name="vtpro.model.OrgAffectPersonnel" table="ORG_AFFECT_PERSONNEL">
        <id name="idAffectPersonnel" type="integer">
            <column name="ID_AFFECT_PERSONNEL" precision="22" scale="0" />
            <generator class="assigned" />
        </id>
        <many-to-one name="orgPoste" class="vtpro.model.OrgPoste" fetch="select">
            <column name="ID_POSTE" precision="22" scale="0" not-null="true" />
        </many-to-one>
        <many-to-one name="orgPersonnel" class="vtpro.model.OrgPersonnel" fetch="select">
            <column name="ID_PERSONNEL" precision="22" scale="0" not-null="true" />
        </many-to-one>
        <property name="dateDebutOccupation" type="date">
            <column name="DATE_DEBUT_OCCUPATION" length="7" not-null="true" />
        </property>
        <property name="dateFinOccupation" type="date">
            <column name="DATE_FIN_OCCUPATION" length="7" />
        </property>
    </class>




Ma requête est :
Code:
from OrgPersonnel as p
where p.compteSesame = 'toto@toto.fr'
and p.orgAffectPersonnels.dateDebutOccupation <= current_date()
and (p.orgAffectPersonnels.dateFinOccupation IS NULL
or p.orgAffectPersonnels.dateFinOccupation >= current_date())


et il me génère :
Code:
select
  orgpersonn0_.ID_PERSONNEL as ID1_4_,
  orgpersonn0_.COMPTE_SESAME as COMPTE2_4_,
from
  VTPRO.ORG_PERSONNEL orgpersonn0_,
  VTPRO.ORG_AFFECT_PERSONNEL orgaffectp1_,
  VTPRO.ORG_AFFECT_PERSONNEL orgaffectp2_,
  VTPRO.ORG_AFFECT_PERSONNEL orgaffectp3_
where
  orgpersonn0_.ID_PERSONNEL=orgaffectp3_.ID_PERSONNEL
  and orgpersonn0_.ID_PERSONNEL=orgaffectp2_.ID_PERSONNEL
  and orgpersonn0_.ID_PERSONNEL=orgaffectp1_.ID_PERSONNEL
  and orgpersonn0_.COMPTE_SESAME='toto@toto.fr'
  and orgaffectp1_.DATE_DEBUT_OCCUPATION<=current_date
  and (
   orgaffectp2_.DATE_FIN_OCCUPATION is null
   or orgaffectp3_.DATE_FIN_OCCUPATION>=current_date
  )



Comme vous pouvez le constater, il génère 3 jointures sur ORG_AFFECT_PERSONNEL, tandis qu'une seule serait suffisante.

Donc je dois avoir un problème quelque part, dans ma requête ou bien dans mon mapping. Mais je ne vois pas très bien




--------------------------------------------------------------------

De plus j'ai d'autres critères à ajouter sur les postes :

Code:
and p.orgAffectPersonnels.orgPoste.posteActif='1'
AND p.orgAffectPersonnels.orgPoste.dateDebutValidite >= current_date()
AND (p.orgAffectPersonnels.orgPoste.dateFinValidite IS NULL
OR p.orgAffectPersonnels.orgPoste.dateFinValidite <= current_date()))


Mais lorsque je teste la requete complete dans hql editor, j'ai :
Code:
"org.hibernate.QueryException: could not resolve property: posteActif of: vtpro.model.OrgAffectPersonnel"


Pourtant je lui dis bien p.orgAffectPersonnels.orgPoste.posteActif

J'en déduis que j'ai un problème dans ma syntaxe mais je n'arrive pas trop à comprendre comment le contourner. La doc ne m'a pas beaucoup aidé.
Je suppose qu'il faut que je fasse quelque chose pour pouvoir accéder à un élément qui se trouve dans un set (p.orgAffectPersonnels). Mais j'ai pas trouvé quoi.



Merci !


Last edited by killergege on Wed Aug 01, 2007 9:12 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 01, 2007 9:11 am 
Newbie

Joined: Wed Aug 01, 2007 4:18 am
Posts: 4
C'est bon... j'ai laissé tomber les jointures implicites et fait des jointures explicites.

Ca marche très bien comme ca...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 01, 2007 9:17 am 
Newbie

Joined: Wed Aug 01, 2007 8:53 am
Posts: 6
Salut,

Et bien non je ne pense pas que ta requête soit mauvaise, pense que tu manipules des objets dans ta requête Hibernate donc lorsque tu écris
Code:
and p.orgAffectPersonnels.dateDebutOccupation <= current_date()

cela veut dire que tu dois bien créer une jointure entre OrgPersonnel et OrgAffectPersonnels.

Tu l'as d'ailleurs déclaré dans tes fichiers de mapping, par exemple dans OrgPersonnel.hbm.xml, tu as un déclaré une collection orgAfftectPersonnels qui te permet de retrouver tous les objets de type OrgAffectPersonnel pour un objet OrgPersonnel, ceci se traduira par une jointure dans ta requête SQL.

Pas de réponse pour ton autre problème.

Cheachwood


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