Mixing declarative beans and annotated beans: org.hibernate.HibernateException No Session found for current thread

人盡茶涼 提交于 2019-12-12 22:41:21

问题


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"?


回答1:


@Transactional what's ur import package

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

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



来源:https://stackoverflow.com/questions/21751049/mixing-declarative-beans-and-annotated-beans-org-hibernate-hibernateexception-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!