简体   繁体   中英

Can't initialize sessionFactory. Hibernate 3 in Spring

this is my ApplicationContext.xml. I can't run my program because of this error:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [ApplicationContext.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

I must say that HibernateVoc extends HibernateDaoSupport. I've looked at several web pages in search of how to use HibernateDaoSupport, wether these are the correct properties to initialize sessionFactory... I've not managed how to work out the problem.

Thanks y'all!

<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.gjt.mm.mysql.Driver" />
<property name="url" value="jdbc:mysql://localhost/voc" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="datasource" />
<property name="mappingResources">
    <list>
        <value>com/ju/voc/domain/words/Word.hbm.xml</value>
        <value>com/ju/voc/domain/categroies/Category.hbm.xml</value>
    </list>
</property>
<property name="hibernateProperties">
    <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    </props>
</property>
</bean>
<bean id="hibernateVoc" class="com.ju.voc.data.HibernateVoc">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

PS I'm using Maven, so I download libraries from there.

You are missing the dependency to hibernate.

Because the spring-orm artifact handles ORM using plain Hibernate, JPA, JDO and iBatis (and it hardly ever makes sense to use them all), all of these depenencies are marked as optional , which means that you have to reference the dependencies in your pom.xml. Here's how to reference the latest stable version of hibernate core:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>3.6.7.Final</version>
</dependency>

The exception you got (java.lang.NoClassDefFoundError) means that you forgot to include a library jar in your application. Try to deep analyze the log trace, and find the missing class in order to include the proper jar. Probably you missed hibernate dependency.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM