当前位置: 首页 > 知识库问答 >
问题:

Hibernate和Spring sessionFactory为空,未创建db

龚博涛
2023-03-14

我在尝试将Spring与Hibernate集成时遇到了一些问题。从日志中,表和数据库似乎已经创建,但我找不到数据库文件,在检查sessionFactory时,我得到了nullpointer异常,我无法真正理解发生了什么。

以下是我的配置文件:

<?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>
    null
<tx:annotation-driven />

<context:component-scan base-package="org.fabrizio.fantacalcio.controller"/>
<!-- Hibernate ..................................... -->
<beans:bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <beans:property name="configLocation" value="classpath:hibernate.cfg.xml" />
</beans:bean>

<beans:bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <beans:property name="sessionFactory" ref="sessionFactory" />
</beans:bean>


<!-- Miei Bean ...................................... --> 

<beans:bean id="utenteDao"
    class="org.fabrizio.fantacalcio.model.dao.UtenteDaoImpl"> 
</beans:bean>
@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();
    }
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

编辑:这是UtenteDaoImpl类,它不做任何事情,因为它继承了BaseDao的东西。

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

}

共有1个答案

申屠项明
2023-03-14

请将SessionFactory更改为protected

@Autowired
protected SessionFactory sessionFactory;
 类似资料:
  • 问题内容: 我可以寻求您的帮助吗?我的代码不适用于使用socket.room创建房间,首先我已经声明了,我想创建房间并将创建的房间名称推送到。我的目标是允许用户创建自己的房间并在可能的情况下将其删除。我在这里使用了Michael Mukhin的代码作为参考。 这是我的server.js的代码 这是客户端index.html中的代码, 我想创建一个房间并将其添加到的列表中。 请帮助我已经阅读过soc

  • 我现在对hibernate有一个很奇怪的问题。在一个表上,它创建了一个引用它自己的外键。该列也是主键。这基本上阻止了我从表中删除任何行。 在日志中我可以看到一行字:

  • 我在每次迭代中都创建一个新线程。并且每个线程都在创建后立即启动。线程调用一个方法“doSomething()”,该方法Hibernate1秒并打印到控制台。我还在for循环中启动线程后打印一条语句,但在打印一次之后,它会在创建所有线程之前先打印所有这些语句。 输出看起来像- 我在想为什么它会输出成这样?为什么在启动thread==“+i”语句之前打印“system.out.println(”“在启

  • 我有三个DBs,其中hibernate只能创建2/3。有人能解释一下为什么hibernate不能创建第三个表“商品”吗 1: 组织。冬眠工具架构。spi。CommandAcceptanceException:通过JDBC语句执行DDL“create table GOODES(ID integer not null,brand varchar(255),desc varchar(255),model

  • 我有两张桌子: 基本上,我想通过id字段定义一个从到的外键,这两个表中的字段是相同的。问题是,当我在IntelliJ Idea中检查数据库时,我在Keys部分没有看到任何外键(检查了两个表),只有它们的PK。这段代码有问题吗?我已经按照另一个类似问题中的建议设置了属性:

  • 在您假设这是一个重复的之前,我知道以下答案(包括其他答案): null 正在运行hbm2ddl架构更新 实际结果: 正在运行hbm2ddl架构导出 Hibernate从控制台输出: