如何在Hibernate';什么是ClassLoaderService? 背景

如何在Hibernate';什么是ClassLoaderService? 背景,hibernate,groovy,classloader,Hibernate,Groovy,Classloader,我有一组Groovy文件,我使用Groovy的类加载器将其解析为类: GroovyClassLoader gcl = new GroovyClassLoader(); while (groovyIter.hasNext()) { File groovyFile = (File) groovyIter.next(); try { Class clazz = gcl.parseClass(groovyFile); } catch (

我有一组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类,我将classloader设置为GroovyClassLoader的实例

  XStream xstream = new XStream();
  xstream.setClassLoader(gcl); 

  // Read the file and get objects
  Object deserialized = xstream.fromXML(file);
一旦我有了反序列化对象,我想使用Hibernate将其存储在数据库中。在我的hibernate.config.xml中有一个映射条目

它映射到我的带注释的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实现这一点

stacktrace见下文:

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
导致

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
然后是前面提到的堆栈跟踪


有什么建议吗?

您使用的是什么版本的Hibernate?我使用的是Hibernate 4.1.7版,从Java的角度来看,您应该做些什么;我不确定Groovy类加载的任何细节(如果有的话)。你能显示堆栈跟踪吗?顺便说一句,这个设置实际上是针对
ClassLoaderService
的,我还使用了一个Jar,它实现了一个JarClassLoader。。。XStream可以,但是如果我设置hibernate类加载器的代码可以,它可能会以某种方式覆盖hibernate的自定义类加载器设置。您必须调试到org.hibernate.internal.util.ReflectHelper#classForName中,并查看为什么它没有通过TCCL查找。
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