这是我第一次做ssh框架整合,但这次的整合No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here这个问题。让我花费了巨大的精力和时间才最终解决。
首先感谢博主:王奕然,是他的博客让我的问题得已解决,文章博客地址:https://blog.youkuaiyun.com/macrohui2017/article/details/56666739
现在说说我的整合过程,希望能给后面遇到此问题的行业伙伴给个借鉴。
首先贴上我的java代码及spring hibernate的配置。
package com.inn.zl.hibernate.test;
import com.alibaba.fastjson.JSON;
import com.inn.zl.hibernate.model.entities.TestEntity_HI;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@Component
public class HibernateTest extends HibernateDaoSupport {
@Autowired
private HibernateTemplate hibernateTemplete;
@Resource
public void setMySessionFactory(SessionFactory sessionFactory) {
this.setSessionFactory(sessionFactory);
}
@Test
public void saveTest(){
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:spring.hibernate.cfg.xml");
TestEntity_HI t = new TestEntity_HI();
t.setClassName("test");
HibernateTemplate hibernateTemplate = (org.springframework.orm.hibernate3.HibernateTemplate) context.getBean("hibernateTemplete");
hibernateTemplate.save(t);
}
public static void main(String[] args) {
HibernateTest t = new HibernateTest();
t.findTest();
}
@Test
public void test1(){
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:spring.hibernate.cfg.xml");
HibernateTest h = (HibernateTest) context.getBean("hibernateTest");
h.findTest();
}
@Test
public void findTest(){
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:spring.hibernate.cfg.xml");
TestEntity_HI t = new TestEntity_HI();
t.setClassName("test");
Map<String, Object> map = new HashMap<String,Object>();
StringBuilder hql = new StringBuilder();
hql.append("from " + t.getClass().getSimpleName());
if (!map.isEmpty()) {
Iterator<String> it = map.keySet().iterator();
String key = (String)it.next();
hql.append(" where " + key + "=:" + key);
while(it.hasNext()) {
key = (String)it.next();
hql.append(" and " + key + "=:" + key);
}
}
HibernateTemplate hibernateTemplate = (org.springframework.orm.hibernate3.HibernateTemplate) context.getBean("hibernateTemplete");
System.out.println(JSON.toJSONString(hql));
Query query = hibernateTemplate.getSessionFactory().getCurrentSession().createQuery(hql.toString());
// Query query = hibernateTemplate.getSessionFactory().openSession().createQuery(hql.toString());
query = this.setParameter(query, map);
// this.querySQL = this.getQuerySql(query.getQueryString());
// return query.list();
System.out.println(JSON.toJSONString(query.list()));
// hibernateTemplate.getSessionFactory().getCurrentSession().createQuery("")
// hibernateTemplate.
// System.out.println(JSON.toJSONString(hibernateTemplate.findByExample("className",t)));
}
private static Query setParameter(Query query, Map<String, Object> parameterMap) {
String[] namedParameters = query.getNamedParameters();
if (null != namedParameters && namedParameters.length > 0) {
if (null != parameterMap) {
Iterator iterator = parameterMap.keySet().iterator();
while(iterator.hasNext()) {
String key = (String)iterator.next();
Object tempObject = parameterMap.get(key);
if (tempObject instanceof List) {
query.setParameterList(key, (List)tempObject);
} else {
query.setParameter(key, parameterMap.get(key));
}
}
}
return query;
} else {
return query;
}
}
}
spring.hibernate配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<context:annotation-config/>
<context:component-scan base-package="com.inn.zl.hibernate.test"/>
<!--<context:component-scan base-package="com.inn.zl.hibernate.test.*.*" />-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:db_jdbc.properties</value>
</property>
</bean>
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<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>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan">
<list>
<value>com.inn.zl.hibernate.model.entities</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="javax.persistence.validation.mode">none</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplete" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 配置事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="*" propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice>
<!-- 那些类的哪些方法参与事务 -->
<aop:config>
<aop:pointcut id="businessService" expression="execution(* com.inn.zl.hibernate.test.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="businessService"/>
</aop:config>
</beans>
我使用的是junit测试和王奕然是一样的,所以关键的地方就在这里。
网上的解决方案很多如 :
1、配置文件添加<tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager"/>或者
<tx:annotation-driven transaction-manager="txManager"/>等
2、hibernate配置文件添加<pro name="current_session_context_class">thread</pro >
等等都不行。
其实我的配置一开始就是对的,只是关键地方在于使用junit和aop:config的使用理解不够。
好,现在说说关键的代码在哪里。
@Test
public void test1(){
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:spring.hibernate.cfg.xml");
HibernateTest h = (HibernateTest) context.getBean("hibernateTest");
h.findTest();
}
@Test
public void findTest(){
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:spring.hibernate.cfg.xml");
TestEntity_HI t = new TestEntity_HI();
t.setClassName("test");
Map<String, Object> map = new HashMap<String,Object>();
StringBuilder hql = new StringBuilder();
hql.append("from " + t.getClass().getSimpleName());
if (!map.isEmpty()) {
Iterator<String> it = map.keySet().iterator();
String key = (String)it.next();
hql.append(" where " + key + "=:" + key);
while(it.hasNext()) {
key = (String)it.next();
hql.append(" and " + key + "=:" + key);
}
}
HibernateTemplate hibernateTemplate = (org.springframework.orm.hibernate3.HibernateTemplate) context.getBean("hibernateTemplete");
System.out.println(JSON.toJSONString(hql));
Query query = hibernateTemplate.getSessionFactory().getCurrentSession().createQuery(hql.toString());
query = this.setParameter(query, map);
System.out.println(JSON.toJSONString(query.list()));
以上代码中findTest方法是我开始做的junit,就是在该方法内运行会一直报:No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here。
其中test()方法是我看了王奕然写的,通过test方法调用findTest()方法是能成功查询数据的。复制一段王奕然的说法:
因为说不出很好的解释方案,所以就大概和大家说说我的解决过程而已。方便以后遇到该问题的码农们能快速解决。
第一次写博客,大家不喜勿喷。