Project

General

Profile

Download (1.67 KB) Statistics
| Branch: | Tag: | Revision:
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.proxy.HibernateProxy;
13
import org.hibernate.proxy.LazyInitializer;
14

    
15
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
16

    
17
/**
18
 * TODO merge with {@link HibernateProxyHelper}
19
 * 
20
 * This class extends the {@link org.hibernate.proxy.HibernateProxyHelper}. 
21
 * Since {@link org.hibernate.proxy.HibernateProxyHelper} is final the {@link #getClassWithoutInitializingProxy(Object)} 
22
 * is repeated here for convenience.
23
 * 
24
 * @author a.kohlbecker
25
 */
26
public class HibernateProxyHelperExtended {
27
	
28
	/**
29
	 * Get the class of an instance or the underlying class
30
	 * of a proxy (without initializing the proxy!). It is
31
	 * almost always better to use the entity name!
32
	 * 
33
	 */
34
	public static Class getClassWithoutInitializingProxy(Object object) {
35
		if (object instanceof HibernateProxy) {
36
			HibernateProxy proxy = (HibernateProxy) object;
37
			LazyInitializer li = proxy.getHibernateLazyInitializer();
38
			return li.getPersistentClass();
39
		}
40
		else {
41
			return object.getClass();
42
		}
43
	}
44
	
45
	/**
46
	 * Unwrap the target instance from the proxy.
47
	 */
48
	public static Object getProxyTarget(Object object){
49
		if(object instanceof HibernateProxy) {
50
			LazyInitializer lazyInitializer = ((HibernateProxy)object).getHibernateLazyInitializer();
51
			return lazyInitializer.getImplementation();
52
		} else {
53
			return object;
54
		}
55
	}
56

    
57
}
(3-3/3)