3
votes
@Repository
public class Init {

    public static void main(String[] args) {

        Init init = new Init();
        init.addUser(init.getSessionFactory());

    }

    private SessionFactory getSessionFactory() {
        ApplicationContext context = new ClassPathXmlApplicationContext(
            new String[] { "Spring_Hibernate.xml" });

        SessionFactory sf = (SessionFactory) context.getBean("sessionFactory");

        return sf;
    }

    @Transactional
    private void addUser(SessionFactory sf) {
        Session session = sf.getCurrentSession();

        User user = new User();
        user.setName("123");
        session.save(user);
        session.close();
        sf.close();
    }
}

xml:

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.warmmer.bean" />
    <property name="hibernateProperties">
        <!-- <value> hibernate.dialect=org.hibernate.dialect.HSQLDialect </value> -->
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.format_sql">true</prop>

            <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop> 
        </props>
    </property>
</bean>


<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

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

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

err: INFO: Using DataSource [org.apache.commons.dbcp.BasicDataSource@6098b14d] of Hibernate SessionFactory for HibernateTransactionManager Exception in thread "main" org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread

If: hibernate.current_session_context_class set 'thread'

then :save is not valid without active transaction

what should I do please?

1

1 Answers

2
votes

You are not creating your "Init" object within the spring context, so spring would never get a chance to wrap a proxy around the method with the annotation that will manage the transaction

Try changing your class to...

package my.pkg;
// Imports etc

@Repository
public class Init {

    @Autowired
    private SessionFactory sessionFactory;

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
            new String[] { "Spring_Hibernate.xml" });
        Init init = context.getBean(Init.class);
        init.addUser();
    }

    @Transactional
    private void addUser() {
        Session session = sessionFactory.getCurrentSession();

        User user = new User();
        user.setName("123");
        session.save(user);
        // session.close(); DON'T NEED THESE!
        // sf.close();
    }
}

Now you might need to add the following to your beans file so that it finds your repository...

<context:component-scan base-package="my.pkg"/>