【问题标题】:Eclipse RCP Spring 3 Hibernate 4 class loading issueEclipse RCP Spring 3 Hibernate 4 类加载问题
【发布时间】:2016-04-05 20:27:10
【问题描述】:

我正在尝试通过子类 LocalSessionFactoryBean 设置注释类。
Bean xml 使用 hibernate 3 加载得很好,直到我更改为 hibernate 4 包。我收到了这个错误:

ClassPathXmlApplicationContext: Exception encountered during context initia
lization - cancelling refresh attempt  

  at org.eclipse.jface.operation.ModalContext$ModalContextThread.run(ModalContext.java:121)
Caused by: java.lang.NoClassDefFoundError: org/hibernate/HibernateException
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2595)
    at java.lang.Class.getConstructor0(Class.java:2895)
    at java.lang.Class.getDeclaredConstructor(Class.java:2066)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:7
8)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapable
BeanFactory.java:1032)
    ... 18 more
Caused by: java.lang.ClassNotFoundException: org.hibernate.HibernateException
    at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:501)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:421)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:412)
    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 24 more

我的 bean.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jms="http://www.springframework.org/schema/jms"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.1.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd" >

    <!-- Transaction Manager Definition -->
    <bean id = "transactionManager"        class = "org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name = "sessionFactory"     ref = "sessionFactory" />
    </bean>


    <bean id="dataSource"            class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    </bean>    

    <bean id="sessionFactory"         class = "customSessionFactory">
        <property name="dataSource"         ref="dataSource" />
        <property name="hibernateProperties">
               <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.jdbc.batch_size">10000</prop>
            <prop key="hibernate.show_sql">false</prop>
            <!-- Cache Properties  -->
            <prop key="hibernate.cache.use_second_level_cache">false</prop>
            <prop key="hibernate.cache.use_query_cache">false</prop>
            <prop key="hibernate.generate_statistics">true</prop>
            <prop key="hibernate.cache.use_structured_entries">true</prop>
            <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
            <prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory</prop>
            <prop key="net.sf.ehcache.configurationResourceName">ehcache.xml</prop>
               </props>
            </property>
    </bean>

customSessionFactorysetAnnotationClass 实体类。 我在这里做错了什么?

【问题讨论】:

    标签: eclipse spring hibernate eclipse-rcp


    【解决方案1】:

    确保您的包导入 org.hibernate 包。 hibernate 3.x 可能不需要这样做。

    在您的 MANIFEST.MF 中,将 org.hibernate 添加到 Import-Package: 部分。

    【讨论】:

    • 谢谢。我已经导入了另一个包,它导入了 org.hibernate 并且具有可见性:=重新导出。所以 org.hibernate 应该在那里。它可能与我用来加载 bean 的 bean ClassPathXmlApplicationContext() 上的类加载器有关。
    • 好的,在我更改添加 Eclipse-BuddyPolicy:registered, ext, global on org.hibernate 包 MANIFEST.MF 后,我没有找到类错误。虽然我遇到了映射问题。不知何故,继承上的注释绑定不起作用,即使使用 @MappedSuperclass。