背景
我有一组 Groovy 文件,我使用 Groovy 的类加载器将其解析为类:
GroovyClassLoader gcl = new GroovyClassLoader();
while (groovyIter.hasNext()) {
File groovyFile = (File) groovyIter.next();
try {
Class clazz = gcl.parseClass(groovyFile);
} catch (IOException ioe) {
log.error("Unable to read file " + groovyFile + " to parse class ", ioe);
}
}
GroovyClassLoader 完成后,我使用 XStream 解析 XML 文件,将 XML 反序列化为 Groovy 对象的不同实例。我现在从一种类型开始:发票。为了让 XStream 可以看到 groovy 类,我将类加载器设置为 GroovyClassLoader 的实例。
XStream xstream = new XStream();
xstream.setClassLoader(gcl);
// Read the file and get objects
Object deserialized = xstream.fromXML(file);
一旦我有了反序列化的对象,我想使用 Hibernate 将它存储在数据库中。在我的 hibernate.config.xml 我有一个映射条目
<mapping class="发票"/>
映射到我带注释的 groovy 类
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.Id
import javax.persistence.GenerationType
import groovy.transform.ToString
import groovy.transform.EqualsAndHashCode
import javax.persistence.Table
import javax.persistence.Column
@ToString
@EqualsAndHashCode
@Entity
@Table(name="invoice")
class invoice {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id
String invoicedate
String invoicenumber
String ponumber
String salesrep
String servicedate
String terms
String duedate
String notes
String subtotal
String discountpercent
String discountamount
String total
String paid
String totaldue
}
问题:
当我尝试打开会话时,找不到课程。
Configuration hibernateConfig = new Configuration();
Properties hibernateConfigProps = hibernateConfig.getProperties();
hibernateConfigProps.put("hibernate.classLoader.application", gcl);
hibernateConfig.setProperties(hibernateConfigProps);
SessionFactory sf = hibernateConfig.configure(new File("hibernate.config.xml")).buildSessionFactory();
Session sess = sf.openSession();
原因:org.hibernate.MappingException:无法加载在 Hibernate 配置条目中声明的类 [invoice]。
我尝试将 hibernate.classLoader.application 属性设置为 GroovyClassLoader 实例,然后在配置中设置该属性,但没有运气。
我的搜索结果表明我可能需要使用 org.hibernate.service.classloading.spi.ClassLoaderService 但我不确定如何使用 Hibernate 执行此操作。
请参阅下面的堆栈跟踪:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.simontuffs.onejar.Boot.run(Boot.java:342)
at com.simontuffs.onejar.Boot.main(Boot.java:168)
Caused by: org.hibernate.MappingException: Unable to load class [ invoice] declared in Hibernate configuration <mapping/> entry
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2139)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2087)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2067)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2020)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1993)
at com.referencelogic.xmlormupload.main.XmlormuploadMain.run(XmlormuploadMain.java:101)
at com.referencelogic.xmlormupload.main.XmlormuploadMain.main(XmlormuploadMain.java:54)
... 6 more
Caused by: java.lang.ClassNotFoundException: invoice
at com.simontuffs.onejar.JarClassLoader.findClass(JarClassLoader.java:713)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at com.simontuffs.onejar.JarClassLoader.loadClass(JarClassLoader.java:630)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at org.hibernate.internal.util.ReflectHelper.classForName(ReflectHelper.java:192)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2136)
... 12 more
尝试按照建议设置线程的上下文类加载器,但 Hibernate 忽略了它。
ClassLoader oldcl = Thread.currentThread().getContextClassLoader();
System.out.println("Old classloader is " + oldcl);
Thread.currentThread().setContextClassLoader(gcl);
ClassLoader newcl = Thread.currentThread().getContextClassLoader();
System.out.println("New classloader is " + newcl);
Configuration hibernateConfig = new Configuration();
System.out.println("Hibernate config class loader is: " + hibernateConfig.getClass().getClassLoader());
结果是
Old classloader is com.simontuffs.onejar.JarClassLoader@5c58d6b0
New classloader is groovy.lang.GroovyClassLoader@3ed4b480
Hibernate config class loader is: com.simontuffs.onejar.JarClassLoader@5c58d6b0
其次是前面提到的堆栈跟踪。
有什么建议么?