minor
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / persistence / dao / hibernate / HibernateProxyHelperExtended.java
1 /**
2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.cdm.persistence.dao.hibernate;
11
12 import org.hibernate.envers.entities.mapper.relation.lazy.proxy.MapProxy;
13 import org.hibernate.proxy.HibernateProxy;
14 import org.hibernate.proxy.LazyInitializer;
15
16 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
17 import eu.etaxonomy.cdm.model.common.CdmBase;
18
19 /**
20 * TODO merge with {@link HibernateProxyHelper}
21 *
22 * This class extends the {@link org.hibernate.proxy.HibernateProxyHelper}.
23 * Since {@link org.hibernate.proxy.HibernateProxyHelper} is final the {@link #getClassWithoutInitializingProxy(Object)}
24 * is repeated here for convenience.
25 *
26 * @author a.kohlbecker
27 */
28 public class HibernateProxyHelperExtended {
29
30 /**
31 * Get the class of an instance or the underlying class
32 * of a proxy (without initializing the proxy!). It is
33 * almost always better to use the entity name!
34 *
35 */
36 public static Class getClassWithoutInitializingProxy(Object object) {
37 if (object instanceof HibernateProxy) {
38 HibernateProxy proxy = (HibernateProxy) object;
39 LazyInitializer li = proxy.getHibernateLazyInitializer();
40 return li.getPersistentClass();
41 }
42 else {
43 return object.getClass();
44 }
45 }
46
47 /**
48 * Unwrap the target instance from the proxy.
49 */
50 public static Object getProxyTarget(Object object){
51 if(object instanceof HibernateProxy) {
52 LazyInitializer lazyInitializer = ((HibernateProxy)object).getHibernateLazyInitializer();
53 return lazyInitializer.getImplementation();
54 } else {
55 return object;
56 }
57 }
58
59 }