注释@Transactional不起作用

时间:2015-06-16 21:09:06

标签: spring hibernate jsf transactional

我正在尝试使用注释@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配置是:

应用-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;
    }

我的国家DAO有@Named注释。有人可以帮帮我吗?

THX。

1 个答案:

答案 0 :(得分:2)

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