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 !