Bonjour,
J'utilise Hibernate Tools. J'ai un objet Patient qui peut avoir des Rdv.
Dans le mapping Patient j'ai :
1 2 3 4 5 6
| <set name="rdvs" table="rdv" inverse="true" lazy="false" fetch="join">
<key>
<column name="id_patient" />
</key>
<one-to-many class="objets.Rdv" />
</set> |
Je commence par charger tous mes patients :
1 2 3 4 5 6 7 8
| Session session = HibernateUtil.currentSession2();
Criteria crit = session.createCriteria(Patient.class)
.addOrder(Order.desc("id"))
.setCacheable(true)
.setCacheMode(CacheMode.NORMAL);
patients=crit.list(); |
Puis je recherche le dernier RDV (qui normalement est en mémoire car chargé dans la précédente requête avec le lazy à false) :
1 2 3 4 5 6 7 8 9
| Session session=HibernateUtil.currentSession2();
Criteria q = session.createCriteria(Rdv.class)
.add(Restrictions.eq("patient", patient))
.add(Restrictions.lt("date", new Date()))
.addOrder(Order.desc("date"))
.setCacheable(true)
.setCacheMode(CacheMode.NORMAL);
rdv= (Rdv) q.uniqueResult(); |
Et là je vois en console que j'ai une requête SQL qui s'en faite pour trouver ce RDV. Or c'est justement le comportement que je veux éviter car j'ai déjà chargé le RDV en cache normalement. Voici la requête :
Hibernate: select this_.id_rdv as id_rdv1_11_0_, this_.id_personnel as id_perso2_11_0_, this_.id_patient as id_patie3_11_0_, this_.date as date4_11_0_, this_.commentaire as commenta5_11_0_ from defib2.rdv this_ where this_.id_patient=? and this_.date<? order by this_.date desc
Partager