我想整合spring-framework-4.1与hibernate4.3.6,applicationContext.xml如下
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
<bean id="dataSource"
class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
p:password="${jdbc.password}" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>demo.entity</value>
</list>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="new*" propagation="REQUIRED" />
<tx:method name="set*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="change*" propagation="REQUIRED" />
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="load*" propagation="REQUIRED" read-only="true" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<context:component-scan base-package="demo.action" />
</beans>
hibernate.cfg.xml文件如下:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="jdbc.batch_size">20</property>
<property name="connection.autocommit">true</property>
<property name="show_sql">true</property>
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">UTF-8</property>
</session-factory>
</hibernate-configuration>
Java文件如下:
package Demo.dao;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class DemoDao<T>{
//Session factory injected by spring context
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
private Session getCurrentSession() {
return sessionFactory.getCurrentSession(); //---37 line
}
......
public List<T> find(String hql, Object[] param) {
Query q = this.getCurrentSession().createQuery(hql); //----61 line
if (param != null && param.length > 0) {
for (int i = 0; i < param.length; i++) {
q.setParameter(i, param[i]);
}
}
return q.list();
}
}
当我运行上面的DemoDao的find方法时,产生如下错误:
Struts has detected an unhandled exception:
Messages: •Could not obtain transaction-synchronized Session for current thread
File: org/springframework/orm/hibernate4/SpringSessionContext.java
Line number: 134
Stacktraces
org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134)
org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
Demo.dao.DemoDao.getCurrentSession(DemoDao.java:37)
Demo.dao.DemoDao.find(DemoDao.java:61)
请问上面的配置文件和Java代码哪里出现了问题,如何修改?谢谢!
相关推荐
报错 org.apache.hadoop.hdfs.BlockMissingException: Could not obtain block 2、百度结果 参考 https://blog.csdn.net/xiaozhaoshigedasb/article/details/88999595 防火墙记得关掉; 查看DataNode是否启动;...
标题“Obtain studio-1.rar”暗示我们正在处理一个与“Obtain studio”项目相关的压缩文件,可能是软件开发工具、编程环境或者相关资源的集合。这个压缩文件由于大小超过50MB,所以被分成了三个部分,这通常是因为...
【标签】"Obtain studio" 暗示这可能与一个名为“Obtain Studio”的开发环境或者工具有关。在软件开发领域,"studio"通常指的是集成开发环境(Integrated Development Environment, IDE),如Android Studio、Visual...
halves = [diff // 2 for diff in differences] if len(halves) > 1: next_difference = halves[-1] // 2 else: next_difference = halves[0] // 2 return numbers[-1] - next_difference number_...
在IT领域,尤其是在文件系统管理和编程中,了解和操作文件属性是至关重要的技能。文件属性提供了关于文件的基本信息,如大小、访问权限、隐藏状态等,这些信息对于管理、保护和理解文件内容至关重要。...
RFID environment, consumers can obtain product-related information of tags by using readers which are installed in mobile phones. However, the tag collision problem is serious in this environment. ...
RELEASE NOTES FOR MICROSOFT(R) TCP/IP-32 FOR WINDOWS(TM) FOR WORKGROUPS 3.11 PLEASE READ THIS ENTIRE DOCUMENT General ------- This product is compatible with, and supported exclusively on, the ...
State-of-the-art sequence labeling systems traditionally require large mounts of task-...We obtain state-of-the-art performance on both datasets — 97.55% accuracy for POS tagging and 91.21% F1 for NER.
#### ORA-00033: Current transaction not in cache - **解释**: 当前事务不在缓存中。 - **解决方案**: 确认事务状态和缓存配置。 #### ORA-00034: Unable to locate current PL/SQL transaction - **解释**: ...
在IT领域,尤其是在Web开发中,用户控件是提高代码重用性和开发效率的重要工具。本文将详细解析一个名为“4步就能搞定上传的多功能上传用户控件”的资源,该控件集成了上传、绑定、下载和查看缩略图等多种功能,非常...
Mask for Windows - PRWin98 v4.00 November 1998 (c) Copyright Peter R. Jupp, 1994-98 ------------------------------------ --------------------- HOW TO VIEW THIS FILE --------------------- To view ...
Disassembler - fix for error emitted for unknown type for target of scope operator. Now, ignore it and continue. Disassembly of an FADT now verifies the input FADT and reports any errors found. Fix ...
FreeRTOS 是一个实时操作系统(RTOS),它被广泛用于嵌入式系统中,特别是在资源有限的微控制器(如 MSP430F5529)上。MSP430F5529 是德州仪器(TI)推出的一款超低功耗微控制器,适合于各种物联网(IoT)和嵌入式...
To make use of either more or less strict isolation levels in applications, locking can be customized for an entire session by setting the isolation level of the session with the SET TRANSACTION ...
Python NLTK库安装Error:Resource u*corpora/gutenberg* not found.-附件资源
This document provides important information that is not included in the Microsoft MS-DOS User's Guide and Reference or in online Help. The readme includes notes on procedures you may have to do ...
在这个名为"Obtain-IP-to-file.rar"的压缩包中,我们主要关注的是`获取IP.c`这个源代码文件,它利用了`System`函数来执行Windows操作系统内置的`ipconfig`命令,从而获取本机的IP地址,并将这些信息写入到指定的文件...
Provides overviews that explain what tasks the services perform and how they relate to each otherOffers specific paths to follow in order to obtain a particular installation resultGets you started ...
在提供的堆栈跟踪中,可以看到`NoSuchElementException: Timeout waiting for idle object`异常,这通常表示线程在等待连接池中的空闲连接时超时,即没有在预设时间内获取到连接。这可能是因为: 1. **连接池大小...