如何使用Hibernate的ClassLoaderService注册自定义类加载器?

时间:2012-10-16 20:53:32

标签: hibernate groovy classloader

背景

我有一组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 =“invoice”/>

映射到我的带注释的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

尝试设置线程的上下文类加载器,但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

接着是前面提到的堆栈跟踪。

有什么建议吗?

0 个答案:

没有答案