|
15 | 15 | */
|
16 | 16 | package org.grails.orm.hibernate.proxy;
|
17 | 17 |
|
18 |
| - |
19 |
| -import org.hibernate.collection.internal.AbstractPersistentCollection; |
| 18 | +import java.io.Serializable; |
| 19 | +import org.grails.datastore.mapping.core.Session; |
| 20 | +import org.grails.datastore.mapping.engine.AssociationQueryExecutor; |
| 21 | +import org.grails.datastore.mapping.proxy.ProxyFactory; |
| 22 | +import org.grails.datastore.mapping.proxy.ProxyHandler; |
| 23 | +import org.grails.datastore.mapping.reflect.ClassPropertyFetcher; |
| 24 | +import org.hibernate.Hibernate; |
20 | 25 | import org.hibernate.collection.spi.PersistentCollection;
|
| 26 | +import org.hibernate.proxy.HibernateProxy; |
| 27 | +import org.hibernate.proxy.HibernateProxyHelper; |
21 | 28 |
|
22 | 29 | /**
|
23 |
| - * Implementation of the ProxyHandler interface for Hibernate. |
| 30 | + * Implementation of the ProxyHandler interface for Hibernate using org.hibernate.Hibernate |
| 31 | + * and HibernateProxyHelper where possible. |
24 | 32 | *
|
25 | 33 | * @author Graeme Rocher
|
26 | 34 | * @since 1.2.2
|
27 | 35 | */
|
28 |
| -public class HibernateProxyHandler extends SimpleHibernateProxyHandler { |
| 36 | +public class HibernateProxyHandler implements ProxyHandler, ProxyFactory { |
29 | 37 |
|
| 38 | + @Override |
30 | 39 | public boolean isInitialized(Object o) {
|
31 |
| - if (o instanceof PersistentCollection) { |
32 |
| - return ((PersistentCollection)o).wasInitialized(); |
| 40 | + return Hibernate.isInitialized(o); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public boolean isInitialized(Object obj, String associationName) { |
| 45 | + try { |
| 46 | + Object proxy = ClassPropertyFetcher.getInstancePropertyValue(obj, associationName); |
| 47 | + return isInitialized(proxy); |
33 | 48 | }
|
| 49 | + catch (RuntimeException e) { |
| 50 | + return false; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public Object unwrap(Object object) { |
| 56 | + return unwrapIfProxy(object); |
| 57 | + } |
34 | 58 |
|
35 |
| - return super.isInitialized(o); |
| 59 | + @Override |
| 60 | + public Serializable getIdentifier(Object o) { |
| 61 | + if (o instanceof HibernateProxy) { |
| 62 | + return ((HibernateProxy)o).getHibernateLazyInitializer().getIdentifier(); |
| 63 | + } |
| 64 | + else { |
| 65 | + //TODO seems we can get the id here if its has normal getId |
| 66 | + return null; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public Class<?> getProxiedClass(Object o) { |
| 72 | + if(o instanceof HibernateProxy) { |
| 73 | + return HibernateProxyHelper.getClassWithoutInitializingProxy(o); |
| 74 | + } |
| 75 | + else { |
| 76 | + return o.getClass(); |
| 77 | + } |
36 | 78 | }
|
37 | 79 |
|
38 | 80 | public Object unwrapIfProxy(Object instance) {
|
39 | 81 | if (instance instanceof PersistentCollection) {
|
40 | 82 | initialize(instance);
|
41 | 83 | return instance;
|
42 | 84 | }
|
43 |
| - |
44 |
| - return super.unwrapIfProxy(instance); |
| 85 | + return Hibernate.unproxy(instance); |
45 | 86 | }
|
46 | 87 |
|
| 88 | + @Override |
47 | 89 | public boolean isProxy(Object o) {
|
48 |
| - return super.isProxy(o) || (o instanceof PersistentCollection); |
| 90 | + return (o instanceof HibernateProxy) || (o instanceof PersistentCollection); |
49 | 91 | }
|
50 | 92 |
|
| 93 | + @Override |
51 | 94 | public void initialize(Object o) {
|
52 |
| - if (o instanceof PersistentCollection) { |
53 |
| - final PersistentCollection col = (PersistentCollection)o; |
54 |
| - if (!col.wasInitialized()) { |
55 |
| - col.forceInitialization(); |
| 95 | + Hibernate.initialize(o); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public <T> T createProxy(Session session, Class<T> type, Serializable key) { |
| 100 | + throw new UnsupportedOperationException("createProxy not supported in HibernateProxyHandler"); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public <T, K extends Serializable> T createProxy(Session session, AssociationQueryExecutor<K, T> executor, K associationKey) { |
| 105 | + throw new UnsupportedOperationException("createProxy not supported in HibernateProxyHandler"); |
| 106 | + } |
| 107 | + |
| 108 | + public Object unwrapProxy(Object proxy) { |
| 109 | + return unwrapIfProxy(proxy); |
| 110 | + } |
| 111 | + |
| 112 | + public HibernateProxy getAssociationProxy(Object obj, String associationName) { |
| 113 | + try { |
| 114 | + Object proxy = ClassPropertyFetcher.getInstancePropertyValue(obj, associationName); |
| 115 | + if (proxy instanceof HibernateProxy) { |
| 116 | + return (HibernateProxy) proxy; |
56 | 117 | }
|
| 118 | + return null; |
| 119 | + } |
| 120 | + catch (RuntimeException e) { |
| 121 | + return null; |
57 | 122 | }
|
58 |
| - super.initialize(o); |
59 | 123 | }
|
60 | 124 | }
|
0 commit comments