Announcement Announcement Module
Collapse
No announcement yet.
No Session found for current thread Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • No Session found for current thread

    I'm new in spring (spring 3.1.2, hibernate 4, mysql 5.1).

    I'm trying to test a simple service by using java configuration:

    HTML Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = { MyConfig .class })
    public class UserServiceTest {
    	
    	
    	@Autowired
    	private UserService userService;
    	
    	@Test
      public void testUserService() {
    		List<User> user = userService.listUsers();
    		assertEquals(2, user.size());
    	}
    }
    But this error is thrown ... I've read several posts with the same error but I didn't find the solution... If someone have any idea , it would be great.

    HTML Code:
    org.hibernate.HibernateException: No Session found for current thread
    	at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
    	at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:941)
    	at com.my.xxx.dao.UserDaoImpl.listUsers(UserDaoImpl.java:29)
    	at com.my.xxx.service.UserServiceImpl.listUsers(UserServiceImpl.java:27)
    	at com.my.xxx.service.UserServiceTest.testUserService(UserServiceTest.java:28)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    	at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

    The java configuration :
    HTML Code:
    @Configuration
    @ComponentScan(basePackages = {"com.my.xxx.dao","com.my.xxx.service"})
    public class MyConfig {
    	private static final Logger logger = LoggerFactory.getLogger(MyConfig.class);
    
    	@Bean
    	public SessionFactory sessionFactory() {
    		SessionFactory mySessionFactory = new LocalSessionFactoryBuilder(datasource()).addAnnotatedClasses(User.class).buildSessionFactory();
    		return mySessionFactory;
    	}
    
    	@Bean
    	public PlatformTransactionManager transactionManager() {
    		return new HibernateTransactionManager(sessionFactory());
    	}
    
    	@Bean
    	public Properties hibernateProperties() {
    		Properties properties = new Properties();
    		properties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
    		properties.put("hibernate.show_sql", "true");
    		properties.put("hibernate.hbm2ddl.auto", "create");
    		return properties;
    	}
    
    	@Bean
    	public DataSource datasource() {
    		Properties props = new Properties();
    		try {
    			props.load(this.getClass().getClassLoader().getResourceAsStream("datasource-xxx.properties"));
    		} catch (IOException e) {
    			logger.error("Error while loading xxx-spring properties", e);
    		}
    		DriverManagerDataSource dataSource = new DriverManagerDataSource();
    		dataSource.setDriverClassName(props.getProperty("database.driver"));
    		dataSource.setUrl(props.getProperty("database.url"));
    		dataSource.setUsername(props.getProperty("database.username"));
    		dataSource.setPassword(props.getProperty("database.password"));
    		dataSource.setConnectionProperties(hibernateProperties());
    		return dataSource;
    	}
    
    }
    My service :
    HTML Code:
    @Service
    @Transactional
    public class UserServiceImpl implements UserService {
    
    	@Autowired
    	private UserDao userDao;
    
    	@Override
    	public void addUser(User user) {
    		userDao.addUser(user);
    
    	}
    
    	@Override
    	public List<User> listUsers() {
    		return userDao.listUsers();
    	}
    
    	@Override
    	public void removeUser(User user) {
    		// TODO Auto-generated method stub
    
    	}
    
    
    }
    My DAO :
    HTML Code:
    @Repository
    @Transactional
    public class UserDaoImpl implements UserDao {
    
    	@Autowired
      private SessionFactory sessionFactory;
    	
    
    	@Override
    	public void addUser(User user) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@SuppressWarnings("unchecked")
    	@Override
    	public List<User> listUsers() {
    		List<User> users = (List<User>)sessionFactory.getCurrentSession().createSQLQuery("select * from Users").list();
    		return (List<User>)  sessionFactory.getCurrentSession().createQuery("from User")
            .list();
    	}
    
    	@Override
    	public void removeUser(User user) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	
    }
    My entity :
    HTML Code:
    @Entity
    @Table(name = "Users")
    public class User implements Serializable {
    
    	@Id
    	@Column(name = "u_id")
    	@GeneratedValue
    	private Integer id;
    	
    	@Column(name="u_lastname")
    	String name;
    	
    	@Column(name="u_firstname")
    	String firstname;
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public String getFirstname() {
    		return firstname;
    	}
    
    	public void setFirstname(String firstname) {
    		this.firstname = firstname;
    	}
    
    }

  • #2

    But this error is thrown ... I've read several posts with the same error but I didn't find the solution... If someone have any idea , it would be great.
    I suggest to improve your search skills and read the transaction chapter of the reference guide.

    Your @Transactional is useless there is nothing that does something with this annotation (annotations by themselves are pretty much useless)... Add @EnableTransactionManagement to your configuration class.

    Comment

    Working...
    X