Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring MVC Hibernate OpenSessionInView配置_Java_Hibernate_Spring Mvc - Fatal编程技术网

Java Spring MVC Hibernate OpenSessionInView配置

Java Spring MVC Hibernate OpenSessionInView配置,java,hibernate,spring-mvc,Java,Hibernate,Spring Mvc,我想添加hibernate的OpenSessionInView功能,因为我可以在模板中访问Lazy OneToMany关联。(我没有找到另一个避免懒惰异常的选项) 最初我没有applicationContext.xml,只有*-servlet.xml。我认为OpenSessionView需要ContextLoaderListener,它需要applicationContext.xml 当我想要部署应用程序时,我会在控制台中获得以下输出: 04-Apr-2016 12:34:54.659 INFO

我想添加hibernate的OpenSessionInView功能,因为我可以在模板中访问Lazy OneToMany关联。(我没有找到另一个避免懒惰异常的选项)

最初我没有applicationContext.xml,只有*-servlet.xml。我认为OpenSessionView需要ContextLoaderListener,它需要applicationContext.xml

当我想要部署应用程序时,我会在控制台中获得以下输出:

04-Apr-2016 12:34:54.659 INFO [RMI TCP Connection(29)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext Root WebApplicationContext: initialization started
04-Apr-2016 12:34:54.740 INFO [RMI TCP Connection(29)-127.0.0.1] org.springframework.web.context.support.XmlWebApplicationContext.prepareRefresh Refreshing Root WebApplicationContext: startup date [Mon Apr 04 12:34:54 CEST 2016]; root of context hierarchy
04-Apr-2016 12:34:54.778 INFO [RMI TCP Connection(29)-127.0.0.1] org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions Loading XML bean definitions from ServletContext resource [/WEB-INF/applicationContext.xml]
04-Apr-2016 12:34:55.083 INFO [RMI TCP Connection(29)-127.0.0.1] org.hibernate.Version.logVersion HHH000412: Hibernate Core {5.1.0.Final}
04-Apr-2016 12:34:55.085 INFO [RMI TCP Connection(29)-127.0.0.1] org.hibernate.cfg.Environment.<clinit> HHH000206: hibernate.properties not found
04-Apr-2016 12:34:55.086 INFO [RMI TCP Connection(29)-127.0.0.1] org.hibernate.cfg.Environment.buildBytecodeProvider HHH000021: Bytecode provider name : javassist
04-Apr-2016 12:34:55.205 INFO [RMI TCP Connection(29)-127.0.0.1] org.hibernate.annotations.common.reflection.java.JavaReflectionManager.<clinit> HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
04-Apr-2016 12:34:55.508 INFO [RMI TCP Connection(29)-127.0.0.1] org.hibernate.dialect.Dialect.<init> HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
04-Apr-2016 12:34:55.870 INFO [RMI TCP Connection(29)-127.0.0.1] org.hibernate.validator.internal.util.Version.<clinit> HV000001: Hibernate Validator 5.1.0.Final
04-Apr-2016 12:34:56.294 INFO [RMI TCP Connection(29)-127.0.0.1] org.springframework.orm.hibernate5.HibernateTransactionManager.afterPropertiesSet Using DataSource [org.apache.commons.dbcp.BasicDataSource@7e9ba] of Hibernate SessionFactory for HibernateTransactionManager
04-Apr-2016 12:34:56.304 INFO [RMI TCP Connection(29)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext Root WebApplicationContext: initialization completed in 1643 ms
04-Apr-2016 12:34:56.314 SEVERE [RMI TCP Connection(29)-127.0.0.1] org.apache.catalina.core.StandardContext.startInternal One or more Filters failed to start. Full details will be found in the appropriate container log file
04-Apr-2016 12:34:56.314 SEVERE [RMI TCP Connection(29)-127.0.0.1] org.apache.catalina.core.StandardContext.startInternal Context [] startup failed due to previous errors
04-Apr-2016 12:34:56.317 INFO [RMI TCP Connection(29)-127.0.0.1] org.springframework.web.context.support.XmlWebApplicationContext.doClose Closing Root WebApplicationContext: startup date [Mon Apr 04 12:34:54 CEST 2016]; root of context hierarchy
04-Apr-2016 12:34:56.319 WARNING [RMI TCP Connection(29)-127.0.0.1] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc The web application [ROOT] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
[2016-04-04 12:34:56,827] Artifact myapp:war exploded: Error during artifact deployment. See server log for details.
04-Apr-2016 12:34:54.659信息[RMI TCP连接(29)-127.0.0.1]org.springframework.web.context.ContextLoader.initWebApplicationContext根WebApplicationContext:初始化已开始
2016年4月4日12:34:54.740信息[RMI TCP连接(29)-127.0.0.1]org.springframework.web.context.support.XmlWebApplicationContext.prepareRefresh刷新根WebApplicationContext:启动日期[Mon-Apr 04 12:34:54 CEST 2016];上下文层次结构的根
2016年4月4日12:34:54.778信息[RMI TCP连接(29)-127.0.0.1]org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions从ServletContext资源加载xml bean定义[/WEB-INF/applicationContext.xml]
2016年4月4日12:34:55.083信息[RMI TCP连接(29)-127.0.0.1]org.hibernate.Version.logVersion HH000412:hibernate核心{5.1.0.Final}
2016年4月4日12:34:55.085信息[RMI TCP连接(29)-127.0.0.1]org.hibernate.cfg.Environment。HH000206:找不到hibernate.properties
2016年4月4日12:34:55.086信息[RMI TCP连接(29)-127.0.0.1]org.hibernate.cfg.Environment.buildBytecodeProvider HH000021:字节码提供程序名称:javassist
2016年4月4日12:34:55.205信息[RMI TCP连接(29)-127.0.0.1]org.hibernate.annotations.common.reflection.java.JavaReflectionManager。HCANN000001:Hibernate Commons注释{5.0.1.Final}
2016年4月4日12:34:55.508信息[RMI TCP连接(29)-127.0.0.1]org.hibernate.dialogue.dialogue。HH000400:使用方言:org.hibernate.dialogue.mysqldialogue
2016年4月4日12:34:55.870信息[RMI TCP连接(29)-127.0.0.1]org.hibernate.validator.internal.util.Version。HV000001:Hibernate验证程序5.1.0.Final
2016年4月4日12:34:56.294信息[RMI TCP连接(29)-127.0.0.1]org.springframework.orm.hibernate5.HibernateTransactionManager.AfterProperties使用数据源设置[org.apache.commons.dbcp]。BasicDataSource@7e9ba]HibernateTransactionManager的Hibernate SessionFactory版本
2016年4月4日12:34:56.304信息[RMI TCP连接(29)-127.0.0.1]org.springframework.web.context.ContextLoader.initWebApplicationContext根WebApplicationContext:初始化在1643毫秒内完成
2016年4月4日12:34:56.314严重[RMI TCP连接(29)-127.0.0.1]org.apache.catalina.core.StandardContext.start内部一个或多个筛选器无法启动。完整的详细信息将在相应的容器日志文件中找到
2016年4月4日12:34:56.314严重[RMI TCP连接(29)-127.0.0.1]org.apache.catalina.core.StandardContext.startInternal Context[]由于以前的错误,启动失败
2016年4月4日12:34:56.317信息[RMI TCP连接(29)-127.0.0.1]org.springframework.web.context.support.XmlWebApplicationContext.doClose关闭根WebApplicationContext:启动日期[Mon-Apr 04 12:34:54 CEST 2016];上下文层次结构的根
04-Apr-2016 12:34:56.319警告[RMI TCP连接(29)-127.0.0.1]org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc web应用程序[ROOT]注册了JDBC驱动程序[com.mysql.JDBC.driver],但在web应用程序停止时无法注销。为了防止内存泄漏,JDBC驱动程序已被强制注销。
[2016-04-04 12:34:56827]工件myapp:战争爆发:工件部署期间出错。有关详细信息,请参阅服务器日志。
我的web.xml看起来像

 <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                                http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

    <display-name>Archetype Created Web Application</display-name>

<filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
    <init-param>
        <param-name>sessionFactoryBeanName</param-name>
        <param-value>sessionFactory</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>hibernateFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

    <filter>
        <filter-name>SetCharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>SetCharacterEncodingFilter</filter-name>
        <url-pattern>*</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>springdispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:mvc-dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:mvc-dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

Web应用程序创建的原型
冬眠过滤器
org.springframework.orm.hibernate5.support.OpenSessionInViewFilter
sessionFactoryBeanName
会话工厂
冬眠过滤器
/*
SetCharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
编码
UTF8
强制编码
真的
SetCharacterEncodingFilter
*
springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*
springdispatcher
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
类路径:mvc-dispatcher-servlet.xml
1.
mvc调度器
/*
上下文配置位置
类路径:mvc-dispatcher-servlet.xml
org.springframework.web.context.ContextLoaderListener
我的springdispatcher-servlet.xml如下所示:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.mypackage" />
    <mvc:annotation-driven />
    <mvc:resources mapping="/resources/**" location="/resources/" /> 

    <bean id="freemarkerConfig"
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath" value="/WEB-INF/templates/" />
    </bean>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="cache" value="false" />
        <property name="contentType" value="text/html;charset=UTF-8" />
        <property name="suffix" value=".ftl" />
        <property name="exposeSpringMacroHelpers" value="true" />
    </bean>

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

    <tx:annotation-driven />

    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/mydb" />
        <property name="username" value="root" />
        <property name="password" value="" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
        <property name="packagesToScan" value="com.mypackage" />
    </bean>

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

最后,applicationContext.xml如下所示:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.mypackage" />
    <mvc:annotation-driven />
    <mvc:resources mapping="/resources/**" location="/resources/" /> 

    <bean id="freemarkerConfig"
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath" value="/WEB-INF/templates/" />
    </bean>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="cache" value="false" />
        <property name="contentType" value="text/html;charset=UTF-8" />
        <property name="suffix" value=".ftl" />
        <property name="exposeSpringMacroHelpers" value="true" />
    </bean>

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

    <tx:annotation-driven />

    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/mydb" />
        <property name="username" value="root" />
        <property name="password" value="" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
        <property name="packagesToScan" value="com.mypackage" />
    </bean>

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

org.hibernate.dialogue.mysqldialogue
真的
编辑:我不知道为什么,但在将这些行移到applicationContext.xml后,它起了作用:

<context:component-scan base-package="com.mypackage" />
    <mvc:annotation-driven />
    <mvc:resources mapping="/resources/**" location="/resources/" /> 


但我不会将我的问题标记为已解决,因为我认为这不是一个干净的解决方案。

您有hibernate配置文件吗?我对hibernate和spring的经验很少,但我有一个hibernate配置文件,它通常看起来像hibernate.cfg.xml。这将是您列出所有jdbc驱动程序属性的地方。您还可以在这里映射类,以便它知道哪些类与数据库匹配。在我的特殊情况下,我不需要applicationContext.xml,因此我无法真正说明您的需要。我想是一个配置