Spring+hibernate,通过注解配置事务管理,spring无法管理事务

ygxcb001 2017-07-27 04:34:15
自己尝试搭了一个springmvc+spring+hibernate项目,在用SessionFactory.getCurrentSession()之后处理数据总是提示

createSQLQuery is not valid without active transaction

这是不是需要我自己手动开启事务,这样的话就失去spring管理事务的意义了么?下面是我的代码,希望大家帮看下是咋回事,感谢感谢



ApplicationContext.xml

<!-- component-scan自动搜索@Component bean, @Controller , @Service , @Repository等标注的类,并检查@Required,@Autowired的属性已被注入 -->
<context:component-scan base-package="com" >
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<context:property-placeholder location="classpath:/jdbc.properties" />

<!-- 配置连接池 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>

<!-- 注入sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" name= "sessionFactory">
<property name="dataSource" ref="dataSource" />
<!-- -->
<property name="packagesToScan">
<list>
<value>com.entity</value>
</list>
</property>

<property name="hibernateProperties">
<props>
<!-- 定义数据库方言 -->
<!--<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>-->
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<!-- 输出sql -->
<prop key="hibernate.show_sql">false</prop>
<!-- 输出格式化的sql -->
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">none</prop>
<!--允许使用二级缓存-->
<prop key="hibernate.cache.use_second_level_cache">false</prop>
<!--选择的缓存器是EhCache-->
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<prop key="javax.persistence.validation.mode">none</prop>
<!--允许使用查询缓存-->
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.connection.pool_size">2048</prop>
<!--以下是解决mysql间歇性失去连接的配置-->
<prop key="hibernate.connection.autoReconnect">true</prop>
<prop key="hibernate.connection.autoReconnectForPools">true</prop>
<prop key="hibernate.connection.is-connection-validation-required">true</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
</bean>

<!-- 用spring管理事务 -->
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- 开启事务注解 -->
<tx:annotation-driven transaction-manager="txManager" proxy-target-class="true" />



mvc-servlet.xml

<!-- 配置自动扫描的包 -->
<context:component-scan base-package="com">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>

<!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->


<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value="/WEB-INF/jsps/"></property>
<property name = "suffix" value = ".jsp"></property>
</bean>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value="/WEB-INF/views/"></property>
<property name = "suffix" value = ".jpg"></property>
</bean>


controller

@Controller
@RequestMapping("/person")
public class PersonController {

@Autowired
public PersonService personService;

@RequestMapping("/addPerson")
public String addPerson(Person person){

System.out.println(person);
personService.addPerson(person);

return "add";
}
}


Service

@Transactional
@Service
public class PersonService {

@Resource
public PersonDao personDao;
@Transactional
public void addPerson(Person person){
personDao.addPerson(person);
}
}


dao

@Repository
public class PersonDao {

@Resource
public SessionFactory sf ;


public void addPerson(Person person){
Session session = sf.getCurrentSession();
Query q = session.createSQLQuery("select * from person ");
List asd = (List)q.list();
System.out.println(asd);
}


...全文
214 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ygxcb001 2017-07-27
  • 打赏
  • 举报
回复
引用 1 楼 pany1209 的回复:
<prop key="hibernate.current_session_context_class">thread</prop>的thread设置为org.springframework.orm.hibernate4.SpringSessionContext试试。。。
谢谢,果然是好用了,但是还是不太懂什么意思,我去查查,谢谢你。
李德胜1995 2017-07-27
  • 打赏
  • 举报
回复
<prop key="hibernate.current_session_context_class">thread</prop>的thread设置为org.springframework.orm.hibernate4.SpringSessionContext试试。。。

81,095

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧