|
|
@@ -1,32 +1,20 @@
|
|
|
package commons.utility.dao;
|
|
|
|
|
|
-import java.beans.PropertyDescriptor;
|
|
|
import java.io.BufferedReader;
|
|
|
import java.io.Reader;
|
|
|
import java.io.Serializable;
|
|
|
|
|
|
-import org.apache.commons.beanutils.PropertyUtils;
|
|
|
import org.hibernate.Hibernate;
|
|
|
-import org.hibernate.HibernateException;
|
|
|
import org.hibernate.Session;
|
|
|
import org.hibernate.SessionFactory;
|
|
|
-import org.hibernate.proxy.HibernateProxy;
|
|
|
-import org.hibernate.proxy.LazyInitializer;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.stereotype.Repository;
|
|
|
|
|
|
import commons.utility.UnproxyObject;
|
|
|
|
|
|
-import java.lang.reflect.Array;
|
|
|
-import java.lang.reflect.InvocationTargetException;
|
|
|
import java.lang.reflect.ParameterizedType;
|
|
|
import java.sql.Clob;
|
|
|
import java.util.ArrayList;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.HashSet;
|
|
|
import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Set;
|
|
|
|
|
|
public class GenericDAO<T, ID extends Serializable>
|
|
|
implements IDAO<T, ID> {
|
|
|
@@ -74,13 +62,6 @@ public class GenericDAO<T, ID extends Serializable>
|
|
|
return entity;
|
|
|
}
|
|
|
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- public T readNoTransaction(ID id) {
|
|
|
- T entity = (T) sessionFactory.getCurrentSession().get(persistentClass,
|
|
|
- id);
|
|
|
- return entity;
|
|
|
- }
|
|
|
-
|
|
|
public T saveOrUpdate(T entity) throws Exception {
|
|
|
boolean tx = startTransaction();
|
|
|
try {
|
|
|
@@ -146,10 +127,6 @@ public class GenericDAO<T, ID extends Serializable>
|
|
|
commit();
|
|
|
}
|
|
|
|
|
|
- public void flush() {
|
|
|
- currentSession().flush();
|
|
|
- }
|
|
|
-
|
|
|
@SuppressWarnings("unchecked")
|
|
|
public List<T> listAll() {
|
|
|
return listAll(false);
|
|
|
@@ -202,23 +179,6 @@ public class GenericDAO<T, ID extends Serializable>
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- public static <T> T initializeAndUnproxy(T entity) {
|
|
|
- if (entity == null) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- if (entity instanceof HibernateProxy) {
|
|
|
- Hibernate.initialize(entity);
|
|
|
- entity = (T) ((HibernateProxy) entity).getHibernateLazyInitializer().getImplementation();
|
|
|
- }
|
|
|
- return entity;
|
|
|
- }
|
|
|
-
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- public T unproxy(T proxied) {
|
|
|
- return deepDeproxy(proxied);
|
|
|
- }
|
|
|
-
|
|
|
public List<T> unproxy(List<T> proxied) {
|
|
|
List<T> unproxied = new ArrayList<T>();
|
|
|
for (T element : proxied)
|
|
|
@@ -226,103 +186,4 @@ public class GenericDAO<T, ID extends Serializable>
|
|
|
return unproxied;
|
|
|
}
|
|
|
|
|
|
- public T deepDeproxy(final Object maybeProxy) throws ClassCastException {
|
|
|
- if (maybeProxy == null)
|
|
|
- return null;
|
|
|
- T ret = deepDeproxy(maybeProxy, new HashSet<Object>());
|
|
|
- return ret;
|
|
|
- }
|
|
|
-
|
|
|
- private T deepDeproxy(final Object maybeProxy, final HashSet<Object> visited)
|
|
|
- throws ClassCastException {
|
|
|
- if (maybeProxy == null)
|
|
|
- return null;
|
|
|
- Class clazz;
|
|
|
- Hibernate.initialize(maybeProxy);
|
|
|
- if (maybeProxy instanceof HibernateProxy) {
|
|
|
- HibernateProxy proxy = (HibernateProxy) maybeProxy;
|
|
|
- LazyInitializer li = proxy.getHibernateLazyInitializer();
|
|
|
- clazz = li.getImplementation().getClass();
|
|
|
- } else {
|
|
|
- clazz = maybeProxy.getClass();
|
|
|
- }
|
|
|
- T ret = (T) deepDeproxy(maybeProxy, clazz);
|
|
|
- if (visited.contains(ret))
|
|
|
- return ret;
|
|
|
- visited.add(ret);
|
|
|
- for (PropertyDescriptor property : PropertyUtils
|
|
|
- .getPropertyDescriptors(ret)) {
|
|
|
- try {
|
|
|
- String name = property.getName();
|
|
|
- if (!"owner".equals(name) && property.getWriteMethod() != null) {
|
|
|
- Object value = PropertyUtils.getProperty(ret, name);
|
|
|
- boolean needToSetProperty = false;
|
|
|
- if (value instanceof HibernateProxy) {
|
|
|
- value = deepDeproxy(value, visited);
|
|
|
- needToSetProperty = true;
|
|
|
- }
|
|
|
- if (value instanceof Object[]) {
|
|
|
- Object[] valueArray = (Object[]) value;
|
|
|
- Object[] result = (Object[]) Array.newInstance(
|
|
|
- value.getClass(), valueArray.length);
|
|
|
- for (int i = 0; i < valueArray.length; i++) {
|
|
|
- result[i] = deepDeproxy(valueArray[i], visited);
|
|
|
- }
|
|
|
- value = result;
|
|
|
- needToSetProperty = true;
|
|
|
- }
|
|
|
- if (value instanceof Set) {
|
|
|
- Set valueSet = (Set) value;
|
|
|
- Set result = new HashSet();
|
|
|
- for (Object o : valueSet) {
|
|
|
- result.add(deepDeproxy(o, visited));
|
|
|
- }
|
|
|
- value = result;
|
|
|
- needToSetProperty = true;
|
|
|
- }
|
|
|
- if (value instanceof Map) {
|
|
|
- Map valueMap = (Map) value;
|
|
|
- Map result = new HashMap();
|
|
|
- for (Object o : valueMap.keySet()) {
|
|
|
- result.put(deepDeproxy(o, visited),
|
|
|
- deepDeproxy(valueMap.get(o), visited));
|
|
|
- }
|
|
|
- value = result;
|
|
|
- needToSetProperty = true;
|
|
|
- }
|
|
|
- if (value instanceof List) {
|
|
|
- List valueList = (List) value;
|
|
|
- List result = new ArrayList(valueList.size());
|
|
|
- for (Object o : valueList) {
|
|
|
- result.add(deepDeproxy(o, visited));
|
|
|
- }
|
|
|
- value = result;
|
|
|
- needToSetProperty = true;
|
|
|
- }
|
|
|
- if (needToSetProperty)
|
|
|
- PropertyUtils.setProperty(ret, name, value);
|
|
|
- }
|
|
|
- } catch (java.lang.IllegalAccessException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } catch (InvocationTargetException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } catch (NoSuchMethodException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- return ret;
|
|
|
- }
|
|
|
-
|
|
|
- private <T> T deepDeproxy(Object maybeProxy, Class<T> baseClass)
|
|
|
- throws ClassCastException {
|
|
|
- if (maybeProxy == null)
|
|
|
- return null;
|
|
|
- if (maybeProxy instanceof HibernateProxy) {
|
|
|
- return baseClass.cast(((HibernateProxy) maybeProxy)
|
|
|
- .getHibernateLazyInitializer().getImplementation());
|
|
|
- } else {
|
|
|
- return baseClass.cast(maybeProxy);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
}
|