Vous devez être membre et vous identifier pour publier un article.
Les visiteurs peuvent toutefois commenter chaque article par une réponse.
Modifier le lazy loading avec Hibernate
Article publié le 28/04/2010 12:08:36Généralités
Cet exemple fonctionne avec la version Hibernate 3.5.1-Final.
Il s’agit d’un aide mémoire pour moi, mais il peut servir à d’autres...
Procédure
RemoteLazyLoadCollectionCallback
- Dans org.hibernate.collection , ajouter le package callback
- Dans org.hibernate.collection.callback, ajouter la classe suivante :
Code Java (11 lignes)
- package org.hibernate.collection.callback;
- import org.hibernate.collection.PersistentCollection;
- import org.hibernate.engine.SessionImplementor;
- public interface RemoteLazyLoadCollectionCallback {
- PersistentCollection lazyLoad(SessionImplementor session,
- PersistentCollection collection);
- }
AbstractPersistentCollection
- Dans org.hibernate.collection, ouvrir la classe AbstractPersistentCollection.
-
Ajouter un membre statique :Code Java (9 lignes)
- private static RemoteLazyLoadCollectionCallback remoteLazyLoadCollectionCallback;
- /**
- * @param remoteLazyLoadCollectionCallback the remoteLazyLoadCollectionCallback to set
- */
- public static synchronized void setRemoteLazyLoadCollectionCallback(
- RemoteLazyLoadCollectionCallback remoteLazyLoadCollectionCallback) {
- AbstractPersistentCollection.remoteLazyLoadCollectionCallback = remoteLazyLoadCollectionCallback;
- }
- Ajouter un getter :
Code Java (3 lignes)
- private boolean isRemote() {
- return remoteLazyLoadCollectionCallback != null;
- }
- Dans la méthode protected boolean readSize(), ajouter au début
Code Java (3 lignes)
- if (isRemote()) {
- initialize(true);
- }
Faire de même pour toutes les méthodes readxxx. - Modifier la méthode protected final void initialize(boolean writing) comme ceci :
Code Java (15 lignes)
- protected final void initialize(boolean writing) {
- if (!initialized) {
- if (isRemote()) {
- remoteLazyLoadCollectionCallback.lazyLoad(session, this);
- setInitialized();
- } else {
- if (initializing) {
- throw new LazyInitializationException(
- "illegal access to loading collection");
- }
- throwLazyInitializationExceptionIfNotConnected();
- session.initializeCollection(this, writing);
- }
- }
- }
RemoteLazyLoadProxyCallback
- Dans org.hibernate.proxy , ajouter le package callback
- Dans org.hibernate.proxy.callback, ajouter la classe suivante :
Code Java (12 lignes)
- package org.hibernate.proxy.callback;
- import java.io.Serializable;
- import org.hibernate.engine.SessionImplementor;
- public interface RemoteLazyLoadProxyCallback {
- }
AbstractLazyInitializer
- Dans org.hibernate.proxy, ouvrir la classe AbstractLazyInitializer
- Ajouter un membre statique :
Code Java (14 lignes)
- private static RemoteLazyLoadProxyCallback remoteLazyLoadProxyCallback;
- /**
- * @param remoteLazyLoadProxyCallback the remoteLazyLoadProxyCallback to set
- */
- public static synchronized void setRemoteLazyLoadProxyCallback(
- RemoteLazyLoadProxyCallback remoteLazyLoadProxyCallback) {
- AbstractLazyInitializer.remoteLazyLoadProxyCallback = remoteLazyLoadProxyCallback;
- }
- Ajouter un getter :
- private boolean isRemote() {
- return (remoteLazyLoadProxyCallback != null);
- }
- Ajouter un getter :
Code Java (3 lignes)
- private boolean isRemote() {
- return (remoteLazyLoadProxyCallback != null);
- }
- Modifier la méthode public final void initialize() comme ceci :
Code Java (26 lignes)
- public final void initialize() throws HibernateException {
- if (!initialized) {
- if (isRemote()) {
- setImplementation(remoteLazyLoadProxyCallback.lazyLoad(session,
- entityName, id));
- } else {
- if ( session==null ) {
- throw new LazyInitializationException("could not initialize proxy - no Session");
- }
- else if ( !session.isOpen() ) {
- throw new LazyInitializationException("could not initialize proxy - the owning Session was closed");
- }
- else if ( !session.isConnected() ) {
- throw new LazyInitializationException("could not initialize proxy - the owning Session is disconnected");
- }
- else {
- target = session.immediateLoad(entityName, id);
- initialized = true;
- checkTargetState();
- }
- }
- }
- else {
- checkTargetState();
- }
- }
Exemple d’appel dans le client
Code Java (4 lignes)
AbstractLazyInitializer .setRemoteLazyLoadProxyCallback(new MeatClientLazyLoadProxyCallback()); AbstractPersistentCollection .setRemoteLazyLoadCollectionCallback(new MeatClientLazyLoadCollectionCallback());
Exemples d'implémentations des callbacks
Code Java (16 lignes)
public class MeatClientLazyLoadProxyCallback implements RemoteLazyLoadProxyCallback { // Thread.dumpStack(); id); if(result == null){ LogFactory.getLog(getClass()).error(entityName+" - "+ id +" is NULL ‹‹‹‹‹‹‹‹‹‹‹‹‹‹‹‹‹‹‹‹‹‹‹‹"); } return result; } }
Code Java (41 lignes)
public class MeatClientLazyLoadCollectionCallback implements RemoteLazyLoadCollectionCallback { public PersistentCollection lazyLoad(SessionImplementor session, PersistentCollection collection) { PersistentCollection result = ServiceLocator.getService( LazyInitFacade.class).initializeCollection(collection, true); Field field; if (collection instanceof PersistentSet) { field = ReflectionUtils.findField(PersistentSet.class, "set", } else if (collection instanceof PersistentBag) { field = ReflectionUtils.findField(PersistentBag.class, "bag", } else if (collection instanceof PersistentList) { field = ReflectionUtils.findField(PersistentList.class, "list", } else if (collection instanceof PersistentMap) { field = ReflectionUtils.findField(PersistentMap.class, "map", } else { .getClass() + " is not yet supported")); } "storedSnapshot"); if (snapshot != null) { ReflectionUtils.makeAccessible(snapshot); if (o == null) { } } ReflectionUtils.makeAccessible(field); ReflectionUtils.setField(field, collection, result); return result; } }
Un article de StephModifié 1 fois. (dernière modification le 03/05/2010 13:22:48 par Steph)
Source : indéterminée
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-455.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.