Good day! I'm having problem making hibernate work on a external jar which is loaded thru reflection on my EJB. As of now I've tried three options but none of these works and just gives me error.
Here are a snippet of codes I've tried. I also added comment to which line the error occurs in each option.
OPTION 1. PASSING SESSION FROM EJB TO EXTERNAL JAR THRU HashMap/Map
My Session Bean Class
public Object processRequest(Map parameters) {
String runnerClass = (String) parameters.get("runnerClass");
String value = (String) parameters.get("value");
String jarPath = "csys.runner.jar";
Object returnObj = null;
try {
parameters.put("sessionEjb", getSession());
File f = new File(jarPath);
URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{f.toURL()},
System.class.getClassLoader());
Class c = urlClassLoader.loadClass(runnerClass);
Method m = c.getMethod("performService", new Class[]{String.class, Map.class});
returnObj = m.invoke(c.newInstance(), new Object[]{value, parameters});
} catch (MalformedURLException | ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
Logger.getLogger(CSysSessionBean.class.getName()).log(Level.SEVERE, null, ex);
throw new RuntimeException(ex);
}
return returnObj;
}
External JAR Method
public Object performService(String value, Map<Object, Object> parameters) {
this.session = (Session) parameters.get("sessionEjb"); // ERROR IN THIS PART ClassCastException
return null;
it gives me this error.
java.lang.ClassCastException: org.hibernate.internal.SessionImpl cannot be cast to org.hibernate.Session
OPTION 2. PASSING SESSION FROM EJB TO EXTERNAL JAR VIA METHOD's PARAMETERS My Session Bean Class
public Object processRequest(Map parameters) {
String runnerClass = (String) parameters.get("runnerClass");
String value = (String) parameters.get("value");
String jarPath = "csys.runner.jar";
Object returnObj = null;
try {
File f = new File(jarPath);
URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{f.toURL()},
System.class.getClassLoader());
Class c = urlClassLoader.loadClass(runnerClass);
Method m = c.getMethod("performService", new Class[]{String.class, Map.class, org.hibernate.Session.class}); //ERROR IN THIS PART NoSuchMethodException
returnObj = m.invoke(c.newInstance(), new Object[]{value, parameters, getSession()});
} catch (MalformedURLException | ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
Logger.getLogger(CSysSessionBean.class.getName()).log(Level.SEVERE, null, ex);
throw new RuntimeException(ex);
}
return returnObj;
}
External JAR Method
public Object performService(String value, Map<Object, Object> parameters, org.hibernate.Session session) {
this.session = session;
return null;
}
now it throws me this error.
java.lang.NoSuchMethodException: csys.server.main.runner.ProjectRunner.performService(java.lang.String, java.util.Map, org.hibernate.Session)
OPTION 3. CREATING SESSION IN EXTERNAL JAR
My Session Bean Class
public Object processRequest(Map parameters) {
String runnerClass = (String) parameters.get("runnerClass");
String value = (String) parameters.get("value");
String jarPath = "csys.runner.jar";
String configPath = "D:/config/hibernate.cfg.xml";
Object returnObj = null;
try {
File f = new File(jarPath);
URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{f.toURL()},
System.class.getClassLoader());
Class c = urlClassLoader.loadClass(runnerClass);
Method m = c.getMethod("performService", new Class[]{String.class, Map.class, String.class});
returnObj = m.invoke(c.newInstance(), new Object[]{value, parameters, configPath});
} catch (MalformedURLException | ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
Logger.getLogger(CSysSessionBean.class.getName()).log(Level.SEVERE, null, ex);
throw new RuntimeException(ex);
}
return returnObj;
}
External JAR Method
public Object performService(String value, Map<Object, Object> parameters, String configPath) {
Configuration configuration = new Configuration();
configuration.configure(new File(configpath)); //ERROR IN THIS PART, COULD NOT PARSE CONFIGURATION
SessionFactory sessionFactory = configuration.buildSessionFactory();
this.session = sessionFactory.openSession();
return null;
}
now it throws me another error.
org.hibernate.HibernateException: Could not parse configuration: D:\config\hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2163)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2133)
Caused by: org.dom4j.DocumentException: org.dom4j.DocumentFactory cannot be cast to org.dom4j.DocumentFactory Nested exception: org.dom4j.DocumentFactory cannot be cast to org.dom4j.DocumentFactory
Note that executing hibernate transactions in my EJB project is working well, it's just executing from external jar gives me error.
Aucun commentaire:
Enregistrer un commentaire