Problème de requêtes sur plusieurs tables (filtre non effectif)
Bonjour.
J'ai un soucis basique avec Hibernate lors de l'utilisation d'une requete sur plusieurs tables.
Ma première requete générée est correcte avec le filtre sur toutes les tables liées (enfants), mais par la suite, si je vais voir les objets enfants, hibernate refait une requete pour avoir la totalité des objets.
Le JAVA a été généré via hibernate generator dans eclipse, mais je en vois pas le soucis.
Exemple:
Un "Personnel" peux avoir des habilitations (table "PersonnelHabilitation"), l'habilitation est dans la table "Habilitation"
Objet Personnel:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
@Entity
@Table(name = "PERSONNEL")
public class Personnel implements java.io.Serializable {
private Integer numpersonnel;
private Set<PersonnelHabilitation> personnelHabilitations
...
@OneToMany(fetch = FetchType.EAGER, mappedBy = "personnel")
public Set<PersonnelHabilitation> getPersonnelHabilitations() {
return this.personnelHabilitations;
}
} |
Objet "PersonnelHabilitation"
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
@Entity
@Table(name = "PERSONNELHABILITATION")
public class PersonnelHabilitation implements java.io.Serializable {
private Habilitation habilitation;
private Personnel personnel;
...
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "NUMHABILITATION")
public Habilitation getHabilitation() {
return this.habilitation;
}
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "NUMPERSONNEL")
public Personnel getPersonnel() {
return this.personnel;
} |
Objet "Habilitation"
Code:
1 2 3 4 5 6 7 8 9 10
|
@Entity
@Table(name = "HABILITATION")
public class Habilitation implements java.io.Serializable {
private String nomhabilitation;
private Set<PersonnelHabilitation> personnelHabilitations
@OneToMany(fetch = FetchType.LAZY, mappedBy = "habilitation")
public Set<PersonnelHabilitation> getPersonnelHabilitations() {
return this.personnelHabilitations;
} |
Lors de ma requete pour avoir l'ensemble des salariés avec tel nom et ayant des formations dont le nom commence par 'San', je fais :
Code:
1 2 3 4 5 6 7 8 9 10 11
|
Criteria query = getHibernateSession().createCriteria(clazz, "personnel");
// filte sur le nom du personnel
query.add(Restrictions.like("personnel.nom", personnel.getNom(),MatchMode.ANYWHERE))
//creation des alias et filtre sur le nom de la formation
query.createAlias("personnel.personnelHabilitations", "personnelHabilitations");
query.createAlias("personnelHabilitations.habilitation", "habilitation");
query.add( Restrictions.not(Restrictions.like("habilitation.nomhabilitation", "San%")));
query.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
return query.list(); |
La requete est bien :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
select
this_.NUMPERSONNEL as NUMPERSONNEL1_2_2_,
this_.NOM as NOM21_2_2_,
...
personnelh1_.NUMHABILITATION as NUMHABILITATION8_3_0_,
personnelh1_.NUMPERSONNEL as NUMPERSONNEL9_3_0_,
....
habilitati2_.NUMHABILITATION as NUMHABILITATION1_0_1_,
habilitati2_.NOMHABILITATION as HABILITATION3_0_1_,
from
INSER.PERSONNEL this_
inner join
INSER.PERSONNELHABILITATION personnelh1_
on this_.NUMPERSONNEL=personnelh1_.NUMPERSONNEL
inner join
INSER.HABILITATION habilitati2_
on personnelh1_.NUMHABILITATION=habilitati2_.NUMHABILITATION
where
(
this_.NOM like 'NOMFILTRE%'
)
and habilitati2_.HABILITATION like 'San%'
; |
Pourtant derrière hibernate, refait la requete suivante
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
select
personnelh0_.NUMPERSONNEL as NUMPERSONNEL9_2_0_,
...
personnelh0_.NUMHABILITATION as NUMHABILITATION8_3_1_,
...
habilitati1_.NUMHABILITATION as NUMHABILITATION1_0_2_,
habilitati1_.NOMHABILITATION as HABILITATION3_0_2_,
from
INSER.PERSONNELHABILITATION personnelh0_
left outer join
INSER.HABILITATION habilitati1_
on personnelh0_.NUMHABILITATION=habilitati1_.NUMHABILITATION
where
personnelh0_.NUMPERSONNEL=? |
--> Cela a pour effet de charger l'ensemble des formations de la personne ayant des formations 'San%' alors que je ne voudrais en résultats aussi que les formations filtrées de la même facon que la première requete. Pourtant, après la première requete, la requete SQL ne retourne que les formations "San%"
Savez vous ce que je fais mal?
Merci d'avance.
Johann