【问题标题】:Mixing declarative beans and annotated beans: org.hibernate.HibernateException No Session found for current thread混合声明性 bean 和带注释的 bean:org.hibernate.HibernateException No Session found for current thread
【发布时间】:2014-02-13 10:07:58
【问题描述】:

我得到“没有为当前线程找到会话”。 我想问题在于混合声明性 xml bean 和带注释的 bean。 接下来,我将恢复我的配置。

我的图书馆项目

春季 3.1.4 休眠 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>

通过组件扫描,我创建了 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 类(在 sn-p 之后),AccountController 在调用 accountService.listAccounts() 时会得到 No Session found for current thread p>

PermissionEvaluator.java (com.mycompany.permissionEvaluator)

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

    @Resource
    private AccountService accountService;

    ...
}

我使用由组件扫描在 model-core-security.xml 中声明的 expressionHandler bean 中创建的 PermissionEvaluator(带有 AccountService、AccountDAO)。

可能是“找不到当前线程的会话”的原因吗?

【问题讨论】:

    标签: spring hibernate session transactional transactionmanager


    【解决方案1】:

    @Transactional 你的导入包是什么

    导入 org.springframework.transaction.annotation.Transactional; ??

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 2013-03-07
      相关资源
      最近更新 更多