View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2010 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.services.persistence.jpa.dao.extentstrategies;
18  
19  import java.util.Collection;
20  
21  import org.apache.commons.collections.map.ReferenceMap;
22  import org.hibernate.Hibernate;
23  import org.hibernate.proxy.HibernateProxy;
24  import org.springframework.dao.DataAccessException;
25  
26  import ch.elca.el4j.services.persistence.hibernate.dao.extent.ExtentCollection;
27  import ch.elca.el4j.services.persistence.hibernate.dao.extent.ExtentEntity;
28  
29  /**
30   * Extent Fetcher for JPA with underlying Hibernate implementation.
31   *
32   * @svnLink $Revision: 4253 $;$Date: 2010-12-21 11:08:04 +0100 (Di, 21. Dez 2010) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/hibernate/src/main/java/ch/elca/el4j/services/persistence/jpa/dao/extentstrategies/HibernateExtentFetcher.java $
33   *
34   * @author Simon Stelling (SST)
35   */
36  public class HibernateExtentFetcher implements ExtentFetcher {
37  
38  	/** {@inheritDoc} */
39  	public void fetchExtentObject(Object object, ExtentEntity entity, ReferenceMap fetchedObjects)
40  		throws DataAccessException {
41  	
42  		Object[] nullArg = null;
43  		if (object == null || entity == null || fetchedObjects == null) {
44  			return;
45  		}
46  		fetchedObjects.put(object, entity);
47  		try {
48  			for (ExtentEntity ent : entity.getChildEntities()) {
49  				Object obj = ent.getMethod().invoke(object, nullArg);
50  				// Initialize the object if it is a proxy
51  				if (obj instanceof HibernateProxy && !Hibernate.isInitialized(obj)) {
52  					Hibernate.initialize(obj);
53  				}
54  				if (!fetchedObjects.containsKey(obj) || !fetchedObjects.get(obj).equals(ent)) {
55  					fetchExtentObject(obj, ent, fetchedObjects);
56  				}
57  			}
58  			
59  			// Fetch the collections. Since we assume batch fetching for collections
60  			for (ExtentCollection c : entity.getCollections()) {
61  				Collection<?> coll = (Collection<?>) c.getMethod().invoke(object, nullArg);
62  				if (coll != null) {
63  					for (Object o : coll) {
64  						// Initialize the object if it is a proxy
65  						if (o instanceof HibernateProxy && !Hibernate.isInitialized(o)) {
66  							Hibernate.initialize(o);
67  						}
68  						if (!fetchedObjects.containsKey(o) || !fetchedObjects.get(o).equals(c.getContainedEntity())) {
69  							fetchExtentObject(o, c.getContainedEntity(), fetchedObjects);
70  						}
71  					}
72  				}
73  			}
74  		} catch (Exception e) {
75  			throw new RuntimeException(e);
76  		}
77  	
78  	}
79  	
80  }