3
votes

I'm Spring and Hibernate newbie. I've created the entity User and DAO - UserHibernateDao. I want to test how the hibernate working in simple java-class with public static void main:

public class Starter {

public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("/org/jtalks/jcommune/model/entity/applicationContext-dao.xml");
    Dao<User> uhd = (Dao) context.getBean("userDao");
    User user = new User();
    user.setNickName("UserName");
    uhd.saveOrUpdate(user);
}

}

but I get the error

INFO: Using DataSource [org.springframework.jdbc.datasource.DriverManagerDataSource@17b4703] of Hibernate SessionFactory for HibernateTransactionManager Exception in thread "main" org.springframework.orm.hibernate3.HibernateSystemException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here; nested exception is 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.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:679)

I understand I need to create something like session and transaction but I don't know how exactly I should do this.

my config User.java UserHibernateDao.java Full Project

Thanks

2

2 Answers

1
votes

Add the @Transactional annotation to your DAO's method.

That will initiate the transactional context.

import org.springframework.transaction.annotation.Transactional;

@Transactional
public User getUser(int id) {
..
}
1
votes

I'd suggest you read this reference on Spring and Transaction Management as it will help you understand how to set this all up.