问题描述
我写测试弹簧应用程序。我有问题。 我的java代码:
$ b $ p
控制器方法
@RequestMapping(value =/ index)
public String setupForm()
{
mainService .getAllRecords());
}
服务界面
@Transactional
public interface MainService
{
public List< Record> getAllRecords();
}
服务impl
<$
@Autowired
private MainDao mainDao ;
@Transactional
public List< Record> getAllRecords()
{
return mainDao.getAllRecords();
}
}
dao不标记为@
我看到下一个错误:
HTTP状态500 - 请求处理失败;嵌套的异常是org.hibernate.HibernateException:没有Hibernate Session绑定到线程,并且配置不允许创建非事务性的一个在这里
如果我将dao接口标记为@Transactional,则效果很好。
配置:
web.xml
< context-param>
< param-name> contextConfigLocation< / param-name>
< param-value> /WEB-INF/spring/root-context.xml< / param-value>
< / context-param>
< listener>
< listener-class> org.springframework.web.context.ContextLoaderListener< / listener-class>
< / listener>
< servlet>
< servlet-name> appServlet< / servlet-name>
< servlet-class> org.springframework.web.servlet.DispatcherServlet< / servlet-class>
< init-param>
< param-name> contextConfigLocation< / param-name>
< param-value> /WEB-INF/spring/appServlet/servlet-context.xml< / param-value>
< / init-param>
1< / load-on-startup>
< / servlet>
< servlet-mapping>
< servlet-name> appServlet< / servlet-name>
< url-pattern> /< / url-pattern>
< / servlet-mapping>
data.xml
<! - 保留所有权利。
< tx:注解驱动的事务管理器=transactionManager/>
<! - Менеджертранзакций - >
< bean id =transactionManagerclass =org.springframework.orm.hibernate3.HibernateTransactionManager>
< property name =sessionFactoryref =sessionFactory/>
< / bean>
< bean id =messageSourceclass =org.springframework.context.support.ReloadableResourceBundleMessageSource>
< property name =basenamevalue =classpath:messages/>
< property name =defaultEncodingvalue =UTF-8/>
< / bean>
< bean id =propertyConfigurerclass =org.springframework.beans.factory.config.PropertyPlaceholderConfigurerp:location =/ WEB-INF / jdbc.properties/>
p:url =$ {jdbc.databaseurl }p:username =$ {jdbc.username}p:password =$ {jdbc.password}/>
< bean id =sessionFactoryclass =org.springframework.orm.hibernate3.LocalSessionFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =configLocation>
< value> classpath:hibernate.cfg.xml< / value>
< / property>
< property name =configurationClass>
< value> org.hibernate.cfg.AnnotationConfiguration< / value>
< / property>
< property name =hibernateProperties>
<道具>
< prop key =hibernate.show_sql> true< / prop>
< prop key =hibernate.dialect> $ {jdbc.dialect}< / prop>
< prop key =hibernate.connection.charSet> UTF-8< / prop>
< prop key =hibernate.hbm2ddl.auto>建立< / prop>
< /道具>
< / property>
< / bean>
root-context.xml
< context:component-scan base-package =com.wp.crud.dao/>
< context:component-scan base-package =com.wp.crud.controller/>
数据访问数据库(数据访问资源)
- >
< import resource =data.xml/>
servlet-context.xml
< annotation-driven />
<! - 所有的资源/ ** - >>的资源和资源/资源/资源/ ** - >
< resource mapping =/ resources / **location =/ resources //>
< beans:bean class =org.springframework.web.servlet.view.InternalResourceViewResolver>
< beans:property name =prefixvalue =/ WEB-INF / views //>
< beans:property name =suffixvalue =。jsp/>
< / beans:bean>
<! - Файлснастройкамиконтроллеров - >
< beans:import resource =controllers.xml/>
controllers.xml
< context:component-scan base-package =com.wp.crud.controller/>
我不明白这个问题的原因。
你能帮我吗?
在你的web.xml中,你缺少声明Hibernate OpenSessionInViewFilter, hibernate session在您的请求过程中打开:
< filter>
< filter-name> hibernateFilter< / filter-name>
< filter-class> org.springframework.orm.hibernate4.support.OpenSessionInViewFilter< / filter-class>
< init-param>
< param-name> singleSession< / param-name>
< param-value> true< /参数值>
< / init-param>
< / filter>
< filter-mapping>
< filter-name> hibernateFilter< / filter-name>
< url-pattern> / *< / url-pattern>
< / filter-mapping>
这可以解决您的错误。
请注意,过滤器类是针对Hibernate4的,您需要修复以防Hibernate3
I write test spring application. I have problem.
my java code:
controller method
@RequestMapping(value = "/index")
public String setupForm()
{
mainService.getAllRecords());
}
service interface
@Transactional
public interface MainService
{
public List<Record> getAllRecords();
}
service impl
@Service("mainService")
@Transactional
public class MainServiceImpl implements MainService
{
@Autowired
private MainDao mainDao;
@Transactional
public List<Record> getAllRecords()
{
return mainDao.getAllRecords();
}
}
dao doesn't mark as @Transactional.
I see next error:
HTTP Status 500 - Request processing failed; nested exception is org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
If I mark dao interface as @Transactional - it works good.
Configuration:
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
data.xml
<!-- Настраивает управление транзакциями с помощью аннотации @Transactional -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Менеджер транзакций -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<!-- Настройки бина dataSource будем хранить в отдельном файле -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:location="/WEB-INF/jdbc.properties" />
<!-- Непосредственно бин dataSource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" />
<!-- Настройки фабрики сессий Хибернейта -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.connection.charSet">UTF-8</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
root-context.xml
<context:component-scan base-package="com.wp.crud.dao"/>
<context:component-scan base-package="com.wp.crud.controller"/>
<!--
Файл с настройками ресурсов для работы с данными (Data Access Resources)
-->
<import resource="data.xml"/>
servlet-context.xml
<annotation-driven />
<!-- Всю статику (изображения, css-файлы, javascript) положим в папку webapp/resources и замаппим их на урл вида /resources/** -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Отображение видов на jsp-файлы, лежащие в папке /WEB-INF/views -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- Файл с настройками контроллеров -->
<beans:import resource="controllers.xml" />
controllers.xml
<context:component-scan base-package="com.wp.crud.controller"/>
I don't understand cause of this problem.
can you help me?
In your web.xml you are missing to declare Hibernate OpenSessionInViewFilter, that provides your hibernate session to be open within your request process:
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
This should fix your error.Please note that the filter class is for Hibernate4, you need to fix in case of Hibernate3
这篇关于HTTP状态500 - 请求处理失败;嵌套异常是org.hibernate.HibernateException:没有Hibernate Session绑定到线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!