混合声明性bean和注释的bean:org.hibernate.HibernateException没有找到当前线程的会话 [英] Mixing declarative beans and annotated beans: org.hibernate.HibernateException No Session found for current thread

查看:143
本文介绍了混合声明性bean和注释的bean:org.hibernate.HibernateException没有找到当前线程的会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到没有找到当前线程的会话。
我想问题在于混合声明式xml bean和带注释的bean。
下面,我将继续我的配置。



MyLibrary项目



Spring 3.1.4
Hibernate 4



applicationContext.xml

 < tx:annotation-driven transaction-manager =transactionManager/> 

< context:component-scan
base-package =com.mycompany.dao.core,com.mycompany.services.core,
com.mycompany.permissionEvaluator />

< import resource =model-core-security.xml/>

< bean id =transactionManager
class =org.springframework.orm.hibernate4.HibernateTransactionManager>
< property name =sessionFactoryref =sessionFactory/>
< / bean>
...(sessionFactory ecc ...)

model-core- security.xml

 < bean id =expressionHandler
class =org.springframework。 security.access.expression.method.DefaultMethodSecurityExpressionHandler>
< property name =permissionEvaluatorref =permissionEvaluator/>
< / bean>

< security:全局方法安全性
pre-post-annotations =enabled>
< security:expression-handler ref =expressionHandler/>
< / security:global-method-security>

使用组件扫描,我创建了Bean:AccountService,AccountDAO和PermissionEvaluator。



AccountService.java(com.mycompany.services)

  @ Service(accountService)
@Transactional
public class AccountServiceImpl implements AccountService {

@Resource
private AccountDAO accountDAO;

...
}

AccountDAO。 java(com.mycompany.dao)

  @Repository 
@Transactional
public class HibernateAccountDAOImpl implements AccountDAO {

... query ...
}

(AccountService e AccountDAO是事务性的)



现在,在 AccountController.java 中,我调用了 accountService.listAccounts()但是,如果我将AccountService注入PermissionEvaluator类(以下代码段),则AccountController将获取没有会话

当调用时 accountService.listAccounts()


$ b

PermissionEvaluator.java(com.mycompany.permissionEvaluator)
$ b

 组件(permissionEvaluator)
公共类PermissionEvaluatorImpl实现PermissionEvaluator {

@Resource
私人帐户服务帐户服务;

...
}

我使用PermissionEvaluator AccountService,AccountDAO)由model-core-security.xml中声明的expressionHandler bean中的component-scan创建。



可能是没有找到currend线程的会话的原因吗?

解决方案您可以通过以下方式联系我们:http://www.businesswire.com/cgi-bin/mmg.cgi?eid = 0>

你可以尝试导入import javax.transaction.Transactional;


I get "No Session found for current thread". I suppose the problem is in mixing declarative xml beans and annotated beans. Following, I'll resume my config.

MyLibrary Project

Spring 3.1.4 Hibernate 4

applicationContext.xml

<tx:annotation-driven transaction-manager="transactionManager" /> 

<context:component-scan 
    base-package="com.mycompany.dao.core, com.mycompany.services.core,
                  com.mycompany.permissionEvaluator" />

<import resource="model-core-security.xml" />

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
... (sessionFactory ecc...)

model-core-security.xml

<bean id="expressionHandler"
class="org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler">
    <property name="permissionEvaluator" ref="permissionEvaluator" />
</bean>

<security:global-method-security
    pre-post-annotations="enabled">
    <security:expression-handler ref="expressionHandler" />
</security:global-method-security>

With component-scan I create beans: AccountService, AccountDAO and PermissionEvaluator.

AccountService.java (com.mycompany.services)

@Service("accountService")
@Transactional
public class AccountServiceImpl implements AccountService {

    @Resource
    private AccountDAO accountDAO;

   ...
}

AccountDAO.java (com.mycompany.dao)

@Repository
@Transactional
public class HibernateAccountDAOImpl implements AccountDAO {

   ...query...
}

(AccountService e AccountDAO are transactional)

Now, within AccountController.java I call accountService.listAccounts() and it's all right!

But, if I inject AccountService into PermissionEvaluator class (following snippet), AccountController gets No Session found for current thread when invokes accountService.listAccounts()

PermissionEvaluator.java (com.mycompany.permissionEvaluator)

Component("permissionEvaluator")
public class PermissionEvaluatorImpl implements PermissionEvaluator {

    @Resource
    private AccountService accountService;

    ...
}

I use PermissionEvaluator (with AccountService, AccountDAO) created by component-scan in expressionHandler bean declared in model-core-security.xml.

Might it be the cause of "no session found for currend thread"?

解决方案

@Transactional what's ur import package

import org.springframework.transaction.annotation.Transactional; ??

U can try to import "import javax.transaction.Transactional;"

这篇关于混合声明性bean和注释的bean:org.hibernate.HibernateException没有找到当前线程的会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆