Annotation @Transactional does not working

188 views Asked by At

I am trying to use the annotation @Transactional to access my MySQL using Hibernate, Spring and JSF. My problem is:

When I use the annotation @Transactional at my managedBean to make a query I got this error:

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)

I don't know what I'm doing wrong. My code and xml config is:

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>

My 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 has the @Named annotation. Someone could help me?

Thx.

1

There are 1 answers

0
M.Faizal On

you have applied @PostConstruct and @Transactional on init method. init method would be called before applying any AOP Proxy interceptor is applied because of @PostConstruct. Hence at the time of invoking init there is no transaction proxy applied. if you need to call init method on Application startup use ApplicationEvent