No cache version.

Caching disabled. Default setting for this page:enabled (code LNG204)
If the display is too slow, you can disable the user mode to view the cached version.

Vous devez être membre et vous identifier pour publier un article.
Les visiteurs peuvent toutefois commenter chaque article par une réponse.

Quand un proxy n’est-il pas un proxy?

Astuces de l’Infobrol (Java)Article publié le 03/05/2011 11:56:22


Si nous utilisons le lazy loading avec Hibernate, nous avons des objets proxy, dont certaines propriétés seulement sont chargées, les autres étant chargées plus tard quand nous en aurons besoin.

Il est facile de déterminer si un objet est de type proxy, par l’instruction suivante
  1. /*...*/
  2. if(proxyCandidate instanceof HibernateProxy)
  3. /*...*/


Par contre, nous rencontrons de nombreux exemples qui utilisent ce type d’instruction pour déterminer si un objet est de type proxy, puis de l’initialiser si c’est le cas. Attention, par défaut ce type de raisonnement est faux car un proxy reste un proxy même une fois qu’il est initialisé (il implémente toujours l’interface).

Il est donc préférable de tester s’il est initialisé, de la manière suivante
  1. /*...*/
  2. proxy.getHibernateLazyInitializer().isUninitialized()
  3. /*...*/


Voici un exemple de méthode qu’il est possible d’invoquer au sein de tests unitaires
  1. /*...*/
  2. private void testProxy(Object proxyCandidate, boolean proxyExpected, Class‹?› clazz) {
  3. if (proxyCandidate != null) {
  4. HibernateProxy proxy = (HibernateProxy) proxyCandidate;
  5. Assert.assertTrue(
  6. String.format(
  7. "%s object %s be initialized at this time",
  8. clazz.getSimpleName(),
  9. proxyExpected ? "must not" : "must"),
  10. proxyExpected == (proxy.getHibernateLazyInitializer().isUninitialized()));
  11. } else
  12. logger.warn(String.format(
  13. "MissionTest.testProxy: %s is null and could not be tested",
  14. clazz.getSimpleName()));
  15. }
  16. /*...*/


Remarques presque hors sujet


Type du proxy


Il existe certains moyens pour s’assurer d’avoir l’implémentation réelle de l’objet, comme ceci :

  1. public static ‹T› T initializeAndUnproxy(T entity) {
  2. if (entity == null) {
  3. throw new
  4. NullPointerException("Entity passed for initialization is null");
  5. }
  6.  
  7. Hibernate.initialize(entity);
  8. if (entity instanceof HibernateProxy) {
  9. entity = (T) ((HibernateProxy) entity).getHibernateLazyInitializer()
  10. .getImplementation();
  11. }
  12. return entity;
  13. }


Et dans ce cas, un proxy n’est plus un proxy :-)

Dangers du proxy


Quand nous utilisons le lazy-loading, nous devons en tenir compte dans notre modèle (la partie indépendante du système de persistance), par exemple dans le cas de la méthode equals, si nous comparons sur des membres qui risquent d’être nulls dans le cas d’un proxy.

Avatar :: Steph Un article de StephModifié 1 fois. (dernière modification le 03/05/2011 11:57:37 par Steph)



Source : indéterminée


Sélection, tri et recherche d'articles
FILTRER :
TRIER :1er critère : 2e critère :
CHERCHER : Dans les titres Dans le contenu


[Afficher les liens en fonction des critères du formulaire ci-dessus]

English translation

You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.

If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.

Thank you in advance.

Document created the 13/09/2004, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/ast-rf-466.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.