简体   繁体   English

Hibernate和Spring sessionFactory null和未创建的数据库

[英]Hibernate and Spring sessionFactory null and db not created

I get some problem while trying to integrate Spring with Hibernate. 尝试将Spring与Hibernate集成时遇到一些问题。 From the log it seems the tables and the database is created, but I can't find the database file and while inspecting the sessionFactory I get nullpointer exception, and I can't really understand what's going on. 从日志看来,似乎已经创建了表和数据库,但是我找不到数据库文件,并且在检查sessionFactory时出现了nullpointer异常,并且我无法真正了解发生了什么。

These are my configuration files: 这些是我的配置文件:

  • web.xml web.xml

     <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Fantacalcio</display-name> <servlet> <servlet-name>spring-mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring-mvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-database.xml,/WEB-INF/spring-mvc-servlet.xml</param-value> </context-param> 

    • spring-mvc-servlet.xml 弹簧-mvc-servlet.xml

<tx:annotation-driven />

<context:component-scan base-package="org.fabrizio.fantacalcio.controller"/>

In my BaseDaoImpl I have the following code: 在我的BaseDaoImpl中,我有以下代码:

@Repository
public class BaseDaoImpl<T> implements BaseDao<T> {

//  private static final Logger logger = LoggerFactory
//          .getLogger(BaseDaoImpl.class);

    @Autowired
    private SessionFactory sessionFactory;

    private Session getSession() {
        return sessionFactory.getCurrentSession();
    }

The log generates these messages, resulting in null sessionFactory and no db file generated: 日志生成以下消息,导致null sessionFactory且没有生成db文件:

18/06/2015 11:20:49 - DEBUG - (StatisticsInitiator.java:110) - Statistics initialized [enabled=true]
18/06/2015 11:20:54 - DEBUG - (SessionFactoryImpl.java:1339) - HHH000031: Closing
18/06/2015 11:20:54 - DEBUG - (ActiveManagementCoordinator.java:149) - The mbean com.mchange.v2.c3p0:type=PooledDataSource,identityToken=2saucp9a1nmzsq198qnlq|1bdc6b2 was not found in the registry, so could not be unregistered.
18/06/2015 11:20:54 - DEBUG - (ActiveManagementCoordinator.java:97) - C3P0Registry mbean unregistered.
18/06/2015 11:20:54 - DEBUG - (BasicResourcePool.java:1022) - Preparing to destroy resource: com.mchange.v2.c3p0.impl.NewPooledConnection@9be45
18/06/2015 11:20:54 - DEBUG - (C3P0PooledConnectionPool.java:616) - Preparing to destroy PooledConnection: com.mchange.v2.c3p0.impl.NewPooledConnection@9be45
18/06/2015 11:20:54 - DEBUG - (AbstractPoolBackedDataSource.java:477) - com.mchange.v2.c3p0.PoolBackedDataSource@1bdc6b2 has been closed. 
java.lang.Exception: DEBUG STACK TRACE for PoolBackedDataSource.close().
    at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.close(AbstractPoolBackedDataSource.java:477)
    at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.close(AbstractPoolBackedDataSource.java:489)
    at com.mchange.v2.c3p0.DataSources.destroy(DataSources.java:372)
    at com.mchange.v2.c3p0.DataSources.destroy(DataSources.java:348)
    at org.hibernate.c3p0.internal.C3P0ConnectionProvider.stop(C3P0ConnectionProvider.java:258)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.stopService(AbstractServiceRegistryImpl.java:377)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.destroy(AbstractServiceRegistryImpl.java:361)
    at org.hibernate.cfg.Configuration$2.sessionFactoryClosed(Configuration.java:1924)
    at org.hibernate.internal.SessionFactoryObserverChain.sessionFactoryClosed(SessionFactoryObserverChain.java:65)
    at org.hibernate.internal.SessionFactoryImpl.close(SessionFactoryImpl.java:1376)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.destroy(LocalSessionFactoryBean.java:488)
    at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:258)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:578)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:554)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:900)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:523)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:907)
    at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:908)
    at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:884)
    at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:836)
    at org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:579)
    at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:115)
    at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:5035)
    at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5687)
    at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
    at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1591)
    at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1580)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
18/06/2015 11:20:54 - DEBUG - (NewPooledConnection.java:646) - com.mchange.v2.c3p0.impl.NewPooledConnection@9be45 closed by a client.
java.lang.Exception: DEBUG -- CLOSE BY CLIENT STACK TRACE
    at com.mchange.v2.c3p0.impl.NewPooledConnection.close(NewPooledConnection.java:646)
    at com.mchange.v2.c3p0.impl.NewPooledConnection.closeMaybeCheckedOut(NewPooledConnection.java:259)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.destroyResource(C3P0PooledConnectionPool.java:619)
    at com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask.run(BasicResourcePool.java:1024)
    at com.mchange.v2.resourcepool.BasicResourcePool.destroyResource(BasicResourcePool.java:1049)
    at com.mchange.v2.resourcepool.BasicResourcePool.destroyResource(BasicResourcePool.java:1010)
    at com.mchange.v2.resourcepool.BasicResourcePool.access$100(BasicResourcePool.java:44)
    at com.mchange.v2.resourcepool.BasicResourcePool$5.run(BasicResourcePool.java:1260)
18/06/2015 11:20:54 - DEBUG - (C3P0PooledConnectionPool.java:627) - Successfully destroyed PooledConnection: com.mchange.v2.c3p0.impl.NewPooledConnection@9be45
18/06/2015 11:20:54 - DEBUG - (BasicResourcePool.java:1027) - Successfully destroyed resource: com.mchange.v2.c3p0.impl.NewPooledConnection@9be45
18/06/2015 11:20:54 - DEBUG - (BasicResourcePool.java:1022) - Preparing to destroy resource: com.mchange.v2.c3p0.impl.NewPooledConnection@532db4
18/06/2015 11:20:54 - DEBUG - (C3P0PooledConnectionPool.java:616) - Preparing to destroy PooledConnection: com.mchange.v2.c3p0.impl.NewPooledConnection@532db4
18/06/2015 11:20:54 - DEBUG - (BootstrapServiceRegistryImpl.java:308) - Implicitly destroying Boot-strap registry on de-registration of all child ServiceRegistries
18/06/2015 11:20:54 - DEBUG - (AbstractServiceRegistryImpl.java:406) - Implicitly destroying ServiceRegistry on de-registration of all child ServiceRegistries

If someone could review my xml code formatting I would appreciate, as It doesn't show properly. 如果有人可以查看我的xml代码格式,我将不胜感激,因为它无法正确显示。

EDIT: This is UtenteDaoImpl class, it does nothing as it inherits stuff from BaseDao. 编辑:这是UtenteDaoImpl类,它不做任何事情,因为它从BaseDao继承了东西。

@Repository
public class UtenteDaoImpl extends BaseDaoImpl<Utente> implements UtenteDao{

}

Please change sessionFactory to protected . 请将sessionFactory更改为protected

@Autowired
protected SessionFactory sessionFactory;

may be useful for you and solve your problem spring and hibernate both are combination this code 可能对您有用,并且可以解决您的问题spring和hibernate都是此代码的组合

<?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:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:annotation-config />

    <mvc:annotation-driven />

    <context:component-scan base-package="com.WordOfTheDay" />

    <mvc:interceptors>
        <bean id="webContentInterceptor"
            class="org.springframework.web.servlet.mvc.WebContentInterceptor">
            <property name="cacheSeconds" value="0" />
            <property name="useExpiresHeader" value="false" />
            <property name="useCacheControlHeader" value="true" />
            <property name="useCacheControlNoStore" value="true" />
        </bean>
    </mvc:interceptors>

    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="/WEB-INF/Messages" />
        <property name="cacheSeconds" value="3000" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager"
        p:sessionFactory-ref="sessionFactory" />

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.connection.CharSet">utf8</prop>
                <prop key="hibernate.connection.characterEncoding">utf8</prop>
                <prop key="hibernate.connection.useUnicode">true</prop>
            </props>
        </property>
        <property name="packagesToScan" value="com.WordOfTheDay"></property>
    </bean>

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql://127.11.115.2:3306/spiritualthesurus?characterEncoding=UTF-8"
        p:username="admin2BXQEV1" p:password="8PcKcsgLWUMR" />

    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10000000000" />
    </bean>

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/WEB-INF/views/" p:suffix=".jsp" />
</beans> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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