org.hibernate.HibernateException:无法获取当前线程的事务同步会话。
将@Transactional添加到服务类后,即使出现此错误。
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean>
<tx:annotation-driven transaction-manager="transactionManager" />
@Service
@Transactional
public class CommonServiceImpl implements CommonService {
@Autowired
private CommonDao commonDao;
public List<FModel> getAll() {
return commonDao.getAll();
}
}
@Repository
public class CommonDaoImpl implements CommonDao {
@Autowired
private SessionFactory sessionFactory;
public List<FModel> getAll() {
List<FModel> list = new ArrayList<FModel>();
Query query = sessionFactory.getCurrentSession().createQuery("from FModel");
list = query.list();
return list;
}
}
这是休眠配置。
<!-- Hibernate session factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<!-- <prop key="hibernate.current_session_context_class">thread</prop> --> <!-- This is for JTA -->
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="packagesToScan" value="com.models" />