[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.rt.eclipselink] Re: EclipseLink+Spring+ComboPooledDataSource+MySQL ORM not persisting

Just a clarification, I am so new to these but I have used Hibernate before. We have over 90 entities and I am using a Delegate class (using Reflection) to access the DAO classes as follows;

------------------------------------------------------------------------------

public class JPADelegateServicesImpl
implements ApplicationContextAware, JPADelegateServices, InitializingBean {


  // The logger
  private static final Log
     logger = LogFactory.getLog(JPADelegateServicesImpl.class);

  private ApplicationContext ctx = null;

  public JPADelegateServicesImpl() { }

  public void afterPropertiesSet() {
     logger.info("Initializing [JPADelegateServicesImpl] bean...");
  }

public Boolean isExists(Class<?> clazz, Object id) throws LeoServerRemoteException {
try {
Object object = getDAOSpringBean(clazz);
Method method = findMethod(object.getClass(), "isExists");


return (Boolean)method.invoke(object, new Object[] { id });
} catch (IllegalAccessException iae) {
throw new LeoServerRemoteException(iae.getMessage(), iae);
} catch (InvocationTargetException ite) {
// Lets see the exception stack and check what has caused this exception
Throwable myThrowable = ite;
while (true) {
if (myThrowable.getCause() != null)
myThrowable = myThrowable.getCause();
else
break;
}
logger.error(myThrowable.getMessage(), myThrowable);
throw new LeoServerRemoteException(myThrowable.getMessage(), myThrowable);
} catch (Exception e) {
throw new LeoServerRemoteException(e.getMessage(), e);
}
}


public Object findById(Class<?> clazz, Object id) throws LeoServerRemoteException {
try {
Object object = getDAOSpringBean(clazz);
Method method = findMethod(object.getClass(), "findById");


Object retObject = method.invoke(object, new Object[] { id });
return retObject;
} catch (IllegalAccessException iae) {
throw new LeoServerRemoteException(iae.getMessage(), iae);
} catch (InvocationTargetException ite) {
// Lets see the exception stack and check what has caused this exception
Throwable myThrowable = ite;
while (true) {
if (myThrowable.getCause() != null)
myThrowable = myThrowable.getCause();
else
break;
}
logger.error(myThrowable.getMessage(), myThrowable);
throw new LeoServerRemoteException(myThrowable.getMessage(), myThrowable);
} catch (Exception e) {
throw new LeoServerRemoteException(e.getMessage(), e);
}
}


  @SuppressWarnings("unchecked")
  public List<Object> findAll(Class<?> clazz, int...rowStartIdxAndCount)
     throws LeoServerRemoteException {
     try {
        Object object = getDAOSpringBean(clazz);
        Method method = findMethod(object.getClass(), "findAll");

Object listObject = method.invoke(object, new Object[] { rowStartIdxAndCount });
return (List<Object>)listObject;
} catch (IllegalAccessException iae) {
throw new LeoServerRemoteException(iae.getMessage(), iae);
} catch (InvocationTargetException ite) {
// Lets see the exception stack and check what has caused this exception
Throwable myThrowable = ite;
while (true) {
if (myThrowable.getCause() != null)
myThrowable = myThrowable.getCause();
else
break;
}
logger.error(myThrowable.getMessage(), myThrowable);
throw new LeoServerRemoteException(myThrowable.getMessage(), myThrowable);
} catch (Exception e) {
throw new LeoServerRemoteException(e.getMessage(), e);
}
}


@SuppressWarnings("unchecked")
public List<Object> findByProperty(Class<?> clazz, String propertyName, Object value,
int...rowStartIdxAndCount) throws LeoServerRemoteException {
try {
Object object = getDAOSpringBean(clazz);
Method method = findMethod(object.getClass(), "findByProperty");


Object listObject =
method.invoke(object, new Object[] { propertyName, value, rowStartIdxAndCount });
return (List<Object>)listObject;
} catch (IllegalAccessException iae) {
throw new LeoServerRemoteException(iae.getMessage(), iae);
} catch (InvocationTargetException ite) {
// Lets see the exception stack and check what has caused this exception
Throwable myThrowable = ite;
while (true) {
if (myThrowable.getCause() != null)
myThrowable = myThrowable.getCause();
else
break;
}
logger.error(myThrowable.getMessage(), myThrowable);
throw new LeoServerRemoteException(myThrowable.getMessage(), myThrowable);
} catch (Exception e) {
throw new LeoServerRemoteException(e.getMessage(), e);
}
}


public void save(Class<?> clazz, Object entity) throws LeoServerRemoteException {
try {
Object object = getDAOSpringBean(clazz);
Method method = findMethod(object.getClass(), "save");


method.invoke(object, new Object[] { entity });
} catch (IllegalAccessException iae) {
throw new LeoServerRemoteException(iae.getMessage(), iae);
} catch (InvocationTargetException ite) {
// Lets see the exception stack and check what has caused this exception
Throwable myThrowable = ite;
while (true) {
if (myThrowable.getCause() != null)
myThrowable = myThrowable.getCause();
else
break;
}
logger.error(myThrowable.getMessage(), myThrowable);
throw new LeoServerRemoteException(myThrowable.getMessage(), myThrowable);
} catch (Exception e) {
throw new LeoServerRemoteException(e.getMessage(), e);
}
}


public void delete(Class<?> clazz, Object entity) throws LeoServerRemoteException {
try {
Object object = getDAOSpringBean(clazz);
Method method = findMethod(object.getClass(), "delete");


method.invoke(object, new Object[] { entity });
} catch (IllegalAccessException iae) {
throw new LeoServerRemoteException(iae.getMessage(), iae);
} catch (InvocationTargetException ite) {
// Lets see the exception stack and check what has caused this exception
Throwable myThrowable = ite;
while (true) {
if (myThrowable.getCause() != null)
myThrowable = myThrowable.getCause();
else
break;
}
logger.error(myThrowable.getMessage(), myThrowable);
throw new LeoServerRemoteException(myThrowable.getMessage(), myThrowable);
} catch (Exception e) {
throw new LeoServerRemoteException(e.getMessage(), e);
}
}


public void update(Class<?> clazz, Object entity) throws LeoServerRemoteException {
try {
Object object = getDAOSpringBean(clazz);
Method method = findMethod(object.getClass(), "update");


method.invoke(object, new Object[] { entity });
} catch (IllegalAccessException iae) {
throw new LeoServerRemoteException(iae.getMessage(), iae);
} catch (InvocationTargetException ite) {
// Lets see the exception stack and check what has caused this exception
Throwable myThrowable = ite;
while (true) {
if (myThrowable.getCause() != null)
myThrowable = myThrowable.getCause();
else
break;
}
logger.error(myThrowable.getMessage(), myThrowable);
throw new LeoServerRemoteException(myThrowable.getMessage(), myThrowable);
} catch (Exception e) {
throw new LeoServerRemoteException(e.getMessage(), e);
}
}


  private Object getDAOSpringBean(Class<?> clazz) {
     String className = clazz.getSimpleName() + "DAO";
     Object object = ctx.getBean(className);
     return object;
  }

@SuppressWarnings("unused")
private Class<?> loadClass(String className) throws ClassNotFoundException {
Class<?> theClass = null;
try {
theClass = Thread.currentThread().getContextClassLoader().loadClass(className);
}
catch (ClassNotFoundException e) {
theClass = Class.forName(className);
}
return theClass;
}


private Method findMethod(Class<?> clazz, String methodName) throws NoSuchMethodException {
Method[] methods = clazz.getMethods();
Method method = null;
boolean methodFound = false;


     for (int i = 0; i < methods.length; i++)
        if (methods[i].getName().equals(methodName)) {
           methodFound = true;
           method = methods[i];
           break;
        }

     if (!methodFound)
        throw new NoSuchMethodException
           ("The method " + methodName + "(...) was not found in class " +
            clazz.getCanonicalName());

     return method;
  }

@Override
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
this.ctx = ctx;
}
}


------------------------------------------------------------------------------

With these class I don't need to expose the DAOs to the client. My main issue is the configuration of Spring and/or Eclipselink to wrap DAO calls in a transaction. All my finders seems to work fine. The log is quite huge but I can see that there are no INSERT statements issued.

Incidentally, we don't use declarative transactions as we call some of the service layers within and spring does not support it.

Thanks in advance