Spring & hibernate 配置(使用 maven):java.lang.ClassNotFoundException:org.hibernate.cfg.Configuration
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2729521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring & hibernate configuration (using maven): java.lang.ClassNotFoundException: org.hibernate.cfg.Configuration
提问by Marcos Carceles
I am trying to include spring and hibernate in an application running on a Weblogic 10.3 server. When I run the application in the server, while accessing an TestServlet to check my configuration I get the following exception:
我试图在 Weblogic 10.3 服务器上运行的应用程序中包含 spring 和 hibernate。当我在服务器中运行应用程序时,在访问 TestServlet 以检查我的配置时,我收到以下异常:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mySessionFactory' defined in class path resource [spring-config/HorizonModelPeopleConnectionsSpringContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.hibernate3.LocalSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org.hibernate.cfg.Configuration
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:448)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:251)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:156)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:284)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:91)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:75)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:65)
at view.com.horizon.test.SpringHibernateServlet.doGet(SpringHibernateServlet.java:27)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:292)
at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at oracle.security.wls.filter.SSOSessionSynchronizationFilter.doFilter(SSOSessionSynchronizationFilter.java:279)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at oracle.dms.wls.DMSServletFilter.doFilter(DMSServletFilter.java:326)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at weblogic.servlet.internal.RequestEventsFilter.doFilter(RequestEventsFilter.java:27)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3592)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2202)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2108)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1432)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:173)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.hibernate3.LocalSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org.hibernate.cfg.Configuration
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:100)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:61)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:756)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:721)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:384)
... 31 more
Caused by: java.lang.NoClassDefFoundError: org.hibernate.cfg.Configuration
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.class$(LocalSessionFactoryBean.java:158)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.(LocalSessionFactoryBean.java:158)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:85)
... 35 more
I have checked my application and the hibernate jar file is included and it contains the class it says its missing: org.hibernate.cfg.Configuration.
我已经检查了我的应用程序,并且包含了 hibernate jar 文件,它包含它说缺少的类:org.hibernate.cfg.Configuration。
The application is built with maven. These are the dependencies of the JAR file using spring and hibernate:
该应用程序是用 maven 构建的。这些是使用 spring 和 hibernate 的 JAR 文件的依赖项:
<!-- Frameworks -->
<!-- Hibernate framework -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.2.7.ga</version>
</dependency>
<!-- Hibernate uses slf4j for logging, for our purposes here use the simple backend -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.2</version>
</dependency>
<!-- Hibernate gives you a choice of bytecode providers between cglib and javassist -->
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.4.GA</version>
</dependency>
<!-- Spring framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>2.5.6</version>
</dependency>
At first I thought it could be an issue with the versions in the spring and hibernate libraries, so I have tried with different ones, but still I couldn't find anywhere where it says which library versions are compatible,. just got that Spring 2.5.x needs hibernate >=3.1
起初我认为这可能是 spring 和 hibernate 库中的版本问题,所以我尝试了不同的版本,但仍然找不到任何说明哪些库版本兼容的地方。刚刚得知 Spring 2.5.x 需要休眠 >=3.1
And this is my Spring config file:
这是我的 Spring 配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="myDataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>jdbc/WebCenterDS</value>
</property>
<!--property name="resourceRef">
<value>true</value>
</property>
<property name="jndiEnvironment">
<props>
<prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop>
<prop key="java.naming.provider.url">t3://localhost:7001</prop>
</props>
</property-->
</bean>
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="configLocation">
<value>classpath:hibernate-config/hibernate.cfg.xml</value>
</property>
<property name="mappingResources">
<list>
<value>classpath:com/horizon/model/peopleconnections/profile/internal/bean/CustomAttribute.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>hibernate.dialect=org.hibernate.dialect.HSQLDialect</value>
</property>
</bean>
<bean id="profileExtensionDAO"
class="com.horizon.model.peopleconnections.profile.internal.dao.ProfileExtensionDAOImpl">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
</beans>
The WAR structure I get is the following:
我得到的 WAR 结构如下:
J2EETestApplication
│ springhibernate.jsp
│
└───WEB-INF
│ faces-config.xml
│ web.xml
│ weblogic.xml
│
├───classes
│ └───view
│ └───com
│ └───horizon
│ └───test
│ SpringHibernateServlet.class
│
└───lib
activation-1.1.jar
antlr-2.7.6.jar
aopalliance-1.0.jar
asm-1.5.3.jar
asm-attrs-1.5.3.jar
cglib-2.1_3.jar
commons-codec-1.3.jar
commons-collections-2.1.1.jar
commons-logging-1.1.1.jar
dom4j-1.6.1.jar
ehcache-1.2.3.jar
hibernate-3.2.7.ga.jar
horizon-model-commons-1.0-SNAPSHOT.jar
horizon-model-peopleconnections-1.0-SNAPSHOT.jar
horizon-shared-commons-1.0-SNAPSHOT.jar
horizon-shared-logging-1.0-SNAPSHOT.jar
horizon-shared-util-1.0-SNAPSHOT.jar
horizon-shared-webcenter-1.0-SNAPSHOT.jar
horizon-shared-webcenter.jar
httpclient-4.0.1.jar
httpcore-4.0.1.jar
javassist-3.4.GA.jar
jta-1.0.1B.jar
log4j-1.2.14.jar
mail-1.4.1.jar
peopleconnections-profile-model-11.1.1.2.0.jar
saxon-9.1.0.8.jar
serviceframework-11.1.1.2.0.jar
slf4j-api-1.5.2.jar
slf4j-log4j12-1.5.2.jar
spring-beans-2.5.6.jar
spring-context-2.5.6.jar
spring-core-2.5.6.jar
spring-orm-2.5.6.jar
spring-tx-2.5.6.jar
Is there any dependency or configuration I am missing?
我缺少任何依赖项或配置吗?
If I use hibernate without spring using (with the same libraries) I don't get the ClassDefNotFoundException:
如果我在没有 spring 的情况下使用 hibernate(使用相同的库),我不会得到 ClassDefNotFoundException:
import java.net.URL;
导入 java.net.URL;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
URL hibernateConfigURL = HibernateUtil.class.getClassLoader().getResource("hibernate-config/hibernate.cfg.xml");
return new Configuration().configure(hibernateConfigURL).buildSessionFactory();
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
That makes me think that is not a problem with the hibernate library not being properly picked. The Spring libraries seem to be picked properly as it starts to build the beans from the ApplicationContent. Could it be an issue of these two libraries not seeing each other?
这让我认为没有正确选择休眠库不是问题。当 Spring 库开始从 ApplicationContent 构建 bean 时,它似乎被正确选择。难道这两个图书馆没有看到对方的问题吗?
回答by khmarbaise
You have missed the hibernate-core dependency...example in the docs.
您错过了 hibernate-core 依赖项...文档中的示例。
http://docs.jboss.org/hibernate/stable/core/reference/en/html/tutorial.html#tutorial-firstapp-mvn
http://docs.jboss.org/hibernate/stable/core/reference/en/html/tutorial.html#tutorial-firstapp-mvn
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
回答by Pascal Thivent
My guess is that your application is not using the Spring JARs inside the webapp but the one provided by WebLogic and those can't "see" the Hibernate JAR inside the war.
我的猜测是您的应用程序没有使用 web 应用程序中的 Spring JAR,而是 WebLogic 提供的那个,而那些无法“看到”战争中的 Hibernate JAR。
To tell Weblogic to look into the war file, provide a WEB-INF/weblogic.xml
with prefer-web-inf-classes
set to true
(this is required for Hibernate anyway because of the ANTLR version bundled in WLS):
告诉Weblogic的寻找到war文件,提供了WEB-INF/weblogic.xml
与prefer-web-inf-classes
设置为true
(这需要对Hibernate反正因为WLS捆绑在一起的ANTLR版本):
<?xml version="1.0" encoding="UTF-8"?>
<weblogic-web-app xmlns="http://www.bea.com/ns/weblogic/90" xmlns:j2ee="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bea.com/ns/weblogic/90 http://www.bea.com/ns/weblogic/90/weblogic-web-app.xsd">
<context-root>/myApp</context-root>
<container-descriptor>
<prefer-web-inf-classes>true</prefer-web-inf-classes>
</container-descriptor>
</weblogic-web-app>
回答by Mohammed Rafeeq
i had compile 'org.hibernate:hibernate-core:4.1.9'
我已经编译 'org.hibernate:hibernate-core:4.1.9'
i replaced it with compile 'org.hibernate:hibernate-core:3.3.2.GA'
我用 compile 'org.hibernate:hibernate-core:3.3.2.GA' 替换它
this is gradle syntax, converting to maven isnt difficult
这是 gradle 语法,转换为 maven 并不困难
回答by Blaze
In addition to your answer @Rafeeq, this did the trick should incase you are using Maven dependency on pom.xml
除了您的回答@Rafeeq,这也可以解决您在 pom.xml 上使用 Maven 依赖的情况
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.3.2.GA</version>
</dependency>