【问题标题】:Annotation @Transactional does not working注释@Transactional 不起作用
【发布时间】:2015-06-16 21:09:06
【问题描述】:

我正在尝试使用注释 @Transactional 来使用 Hibernate、Spring 和 JSF 访问我的 MySQL。我的问题是:

当我在 managedBean 上使用注解 @Transactional 进行查询时,我得到了这个错误:

org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:65)
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:687)
at br.com.rpg.DAO.AbstractDAO.getCurrentSession(AbstractDAO.java:14)
at br.com.rpg.DAO.CountryDAO.findAll(CountryDAO.java:14)
at br.com.rpg.managedBeans.SignupBean.init(SignupBean.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)

我不知道我做错了什么。我的代码和 xml 配置是:

application-config.xml

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="br.com.rpg.DO" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">false</prop>
        </props>
    </property>
</bean>

<context:component-scan base-package="br.com.rpg" />

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

<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

我的 MB

@Named
@Scope("request")
public class SignupBean implements Serializable {

    private static final long serialVersionUID = 1787096549063029840L;

    @Inject
    private CountryDAO country;

    @Inject
    private UserDAO user;

    private Map<String, Integer> countries;

    private Integer selected;

    private String username;

    private String password;

    @PostConstruct
    @Transactional
    public void init() {
        List<CountryDO> findAll = country.findAll();
        countries = new HashMap<String, Integer>();
        for (CountryDO countryDO : findAll) {
            countries.put(countryDO.getName(), countryDO.getId());
        }
    }

    public Map<String, Integer> getCountries() {
        return countries;
    }

    public void setCountries(Map<String, Integer> countries) {
        this.countries = countries;
    }

    public Integer getSelected() {
        return selected;
    }

    public void setSelected(Integer selected) {
        this.selected = selected;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

My Country DAO 有 @Named 注释。有人可以帮助我吗?

谢谢。

【问题讨论】:

  • @Named 是 CDI 工件,@Scope 是 Spring 工件 - 一个令人讨厌的组合 - 不能依赖的东西。不是吗?另一方面,@Transactional 是在服务层上使用的不同的东西。然而,我选择对此保持沉默。

标签: spring hibernate jsf transactional


【解决方案1】:

您已在init 方法上应用了@PostConstruct@Transactional。由于@PostConstruct,在应用任何AOP代理拦截器之前将调用init方法。因此在调用init 时没有应用事务代理。如果您需要在应用程序启动时调用 init 方法,请使用 ApplicationEvent

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-07
    • 1970-01-01
    • 1970-01-01
    • 2019-06-15
    相关资源
    最近更新 更多