美好的一天!我在使休眠在通过我的EJB上的反射加载的外部jar上工作时遇到问题。
到目前为止,我已经尝试了三个选项,但是这些都不起作用,只是给了我错误。
这是我尝试过的代码片段。
我还添加了注释,每个选项在哪行发生错误。
选项1.从EJB到HARMap / Map的外部JAR传递会话
我的会话Bean类
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;
}
外部JAR方法
public Object performService(String value, Map<Object, Object> parameters) {
this.session = (Session) parameters.get("sessionEjb"); // ERROR IN THIS PART ClassCastException
return null;
它给了我这个错误。
java.lang.ClassCastException: org.hibernate.internal.SessionImpl cannot be cast to org.hibernate.Session
选项2.通过方法的参数将会话从EJB传递到外部JAR
我的会话Bean类
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;
}
外部JAR方法
public Object performService(String value, Map<Object, Object> parameters, org.hibernate.Session session) {
this.session = session;
return null;
}
现在,它引发了我这个错误。
java.lang.NoSuchMethodException: csys.server.main.runner.ProjectRunner.performService(java.lang.String, java.util.Map, org.hibernate.Session)
选项3.在外部JAR中创建会话
我的会话Bean类
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;
}
外部JAR方法
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;
}
现在,它引发了另一个错误。
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
请注意,在我的EJB项目中执行休眠事务运行良好,只是从外部jar执行会给我带来错误。
最佳答案
已经找到修复程序。
问题出在我的ClassLoader。
System.class.getClassLoader()
应该
this.getClass().getClassLoader()