Revision cd02d8bd
Added by Andreas Müller over 8 years ago
cdmlib-model/src/main/java/eu/etaxonomy/cdm/hibernate/HibernateProxyHelper.java | ||
---|---|---|
1 |
/** |
|
2 |
* Copyright (C) 2007 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.hibernate; |
|
11 |
|
|
12 |
import java.io.Serializable; |
|
13 |
|
|
14 |
import org.apache.log4j.Logger; |
|
15 |
import org.hibernate.proxy.HibernateProxy; |
|
16 |
import org.hibernate.proxy.LazyInitializer; |
|
17 |
|
|
18 |
/** |
|
19 |
* @author a.mueller |
|
20 |
* @created 03.03.2009 |
|
21 |
* @version 1.0 |
|
22 |
*/ |
|
23 |
public class HibernateProxyHelper { |
|
24 |
@SuppressWarnings("unused") |
|
25 |
private static final Logger logger = Logger.getLogger(HibernateProxyHelper.class); |
|
26 |
|
|
27 |
|
|
28 |
// ************************** Hibernate proxies *******************/ |
|
29 |
/* (non-Javadoc) |
|
30 |
* @see eu.etaxonomy.cdm.model.common.IProxyHelper#deproxy(java.lang.Object, java.lang.Class) |
|
31 |
*/ |
|
32 |
public static <T> T deproxy(Object object, Class<T> clazz) throws ClassCastException { |
|
33 |
if (object instanceof HibernateProxy) { |
|
34 |
return clazz.cast(((HibernateProxy) object).getHibernateLazyInitializer().getImplementation()); |
|
35 |
} else { |
|
36 |
return clazz.cast(object); |
|
37 |
} |
|
38 |
} |
|
39 |
|
|
40 |
/** |
|
41 |
* Unwrap the target instance from the proxy. |
|
42 |
*/ |
|
43 |
public static Object deproxy(Object object){ |
|
44 |
if(object instanceof HibernateProxy) { |
|
45 |
LazyInitializer lazyInitializer = ((HibernateProxy)object).getHibernateLazyInitializer(); |
|
46 |
return lazyInitializer.getImplementation(); |
|
47 |
} else { |
|
48 |
return object; |
|
49 |
} |
|
50 |
} |
|
51 |
|
|
52 |
|
|
53 |
public static boolean isInstanceOf(Object object, Class clazz) throws ClassCastException { |
|
54 |
if (clazz == null){ |
|
55 |
return false; |
|
56 |
} |
|
57 |
if (object instanceof HibernateProxy) { |
|
58 |
Object impl = ((HibernateProxy) object).getHibernateLazyInitializer().getImplementation(); |
|
59 |
Class implClass = impl.getClass(); |
|
60 |
return clazz.isAssignableFrom(implClass); |
|
61 |
} else { |
|
62 |
return clazz.isAssignableFrom(object.getClass()); |
|
63 |
} |
|
64 |
} |
|
65 |
|
|
66 |
public static Serializable getIdentifierOf(Object object) { |
|
67 |
if (object instanceof HibernateProxy) { |
|
68 |
return ((HibernateProxy) object).getHibernateLazyInitializer().getIdentifier(); |
|
69 |
} else { |
|
70 |
throw new ClassCastException("Cannot cast the given Object to a known Hibernate proxy."); |
|
71 |
} |
|
72 |
} |
|
73 |
|
|
74 |
/** |
|
75 |
* Get the class of an instance or the underlying class |
|
76 |
* of a proxy (without initializing the proxy!). It is |
|
77 |
* almost always better to use the entity name! |
|
78 |
* |
|
79 |
* delegates calls to {@link org.hibernate.proxy.HibernateProxyHelper} |
|
80 |
*/ |
|
81 |
public static Class getClassWithoutInitializingProxy(Object object) { |
|
82 |
return org.hibernate.proxy.HibernateProxyHelper.getClassWithoutInitializingProxy(object); |
|
83 |
} |
|
84 |
} |
|
1 |
/** |
|
2 |
* Copyright (C) 2007 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.hibernate; |
|
11 |
|
|
12 |
import java.io.Serializable; |
|
13 |
|
|
14 |
import org.apache.log4j.Logger; |
|
15 |
import org.hibernate.proxy.HibernateProxy; |
|
16 |
import org.hibernate.proxy.LazyInitializer; |
|
17 |
|
|
18 |
/** |
|
19 |
* @author a.mueller |
|
20 |
* @created 03.03.2009 |
|
21 |
* @version 1.0 |
|
22 |
*/ |
|
23 |
public class HibernateProxyHelper { |
|
24 |
@SuppressWarnings("unused") |
|
25 |
private static final Logger logger = Logger.getLogger(HibernateProxyHelper.class); |
|
26 |
|
|
27 |
|
|
28 |
// ************************** Hibernate proxies *******************/ |
|
29 |
/* (non-Javadoc) |
|
30 |
* @see eu.etaxonomy.cdm.model.common.IProxyHelper#deproxy(java.lang.Object, java.lang.Class) |
|
31 |
*/ |
|
32 |
public static <T> T deproxy(Object object, Class<T> clazz) throws ClassCastException { |
|
33 |
if (object instanceof HibernateProxy) { |
|
34 |
return clazz.cast(((HibernateProxy) object).getHibernateLazyInitializer().getImplementation()); |
|
35 |
} else { |
|
36 |
return clazz.cast(object); |
|
37 |
} |
|
38 |
} |
|
39 |
|
|
40 |
/** |
|
41 |
* Unwrap the target instance from the proxy. |
|
42 |
*/ |
|
43 |
public static Object deproxy(Object object){ |
|
44 |
if(object instanceof HibernateProxy) { |
|
45 |
LazyInitializer lazyInitializer = ((HibernateProxy)object).getHibernateLazyInitializer(); |
|
46 |
return lazyInitializer.getImplementation(); |
|
47 |
} else { |
|
48 |
return object; |
|
49 |
} |
|
50 |
} |
|
51 |
|
|
52 |
|
|
53 |
public static boolean isInstanceOf(Object object, Class clazz) throws ClassCastException { |
|
54 |
if (clazz == null){ |
|
55 |
return false; |
|
56 |
} |
|
57 |
if (object instanceof HibernateProxy) { |
|
58 |
Object impl = ((HibernateProxy) object).getHibernateLazyInitializer().getImplementation(); |
|
59 |
Class<?> implClass = impl.getClass(); |
|
60 |
return clazz.isAssignableFrom(implClass); |
|
61 |
} else { |
|
62 |
return clazz.isAssignableFrom(object.getClass()); |
|
63 |
} |
|
64 |
} |
|
65 |
|
|
66 |
public static Serializable getIdentifierOf(Object object) { |
|
67 |
if (object instanceof HibernateProxy) { |
|
68 |
return ((HibernateProxy) object).getHibernateLazyInitializer().getIdentifier(); |
|
69 |
} else { |
|
70 |
throw new ClassCastException("Cannot cast the given Object to a known Hibernate proxy."); |
|
71 |
} |
|
72 |
} |
|
73 |
|
|
74 |
/** |
|
75 |
* Get the class of an instance or the underlying class |
|
76 |
* of a proxy (without initializing the proxy!). It is |
|
77 |
* almost always better to use the entity name! |
|
78 |
* |
|
79 |
* delegates calls to {@link org.hibernate.proxy.HibernateProxyHelper} |
|
80 |
*/ |
|
81 |
public static Class getClassWithoutInitializingProxy(Object object) { |
|
82 |
return org.hibernate.proxy.HibernateProxyHelper.getClassWithoutInitializingProxy(object); |
|
83 |
} |
|
84 |
} |
Also available in: Unified diff
Fix bug for embeddable classes in AdvancedBeanInitializer #5383