Hi i got this Exception:
I found no information in this forum or WWW to solve the Problem.. pls help meorg.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
org.springframework.orm.hibernate3.SpringSessionCo ntext.currentSession(SpringSessionContext.java:63)
org.hibernate.impl.SessionFactoryImpl.getCurrentSe ssion(SessionFactoryImpl.java:687)
de.fzi.hiwitool.db.tables.contract.dao.ContractDAO Impl.save(ContractDAOImpl.java:51)
de.fzi.hiwitool.db.tables.contract.service.Contrac tServiceImpl.addContract(ContractServiceImpl.java: 24)
de.fzi.hiwitool.controllers.HomeController.home(Ho meController.java:49)
sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
...
My configuration:
root-context.xml
servlet-context.xmlCode:<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xsi:schemaLocation=" http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- SEE DatabaseConfiguration.java --> <tx:annotation-driven transaction-manager="transactionManager" /> <context:component-scan base-package="com.mycomp.test" /> </beans>
DatabaseConfiguration.javaCode:<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- Enables the Spring MVC @Controller programming model --> <annotation-driven /> <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> <resources mapping="/resources/**" location="/resources/" /> <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <beans:property name="prefix" value="/WEB-INF/views/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean> <context:component-scan base-package="com.mycomp.test" /> <context:annotation-config /> </beans:beans>
DAO/Service DesignCode:import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.springframework.orm.hibernate3.HibernateTransactionManager; import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean; import com.mysql.jdbc.Driver; @Configuration public class DatabaseConfiguration { @Bean public DataSource myDataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName(Driver.class.getName()); dataSource.setUrl("jdbc:mysql://localhost/test_db"); dataSource.setUsername("root"); dataSource.setPassword(""); return dataSource; } @Bean public AnnotationSessionFactoryBean sessionFactoryBean() { Properties props = new Properties(); props.put("hibernate.dialect", UTF8MysqlDialect.class.getName()); props.put("hibernate.format_sql", "true"); props.put("hibernate.connection.charSet", "UTF-8"); props.put("hibernate.connection.useUnicode", "true"); props.put("hibernate.connection.characterEncoding", "UTF-8"); AnnotationSessionFactoryBean bean = new AnnotationSessionFactoryBean(); bean.setPackagesToScan(new String[]{"com.mycomp.test.db.tables"}); bean.setHibernateProperties(props); bean.setDataSource(myDataSource()); bean.setSchemaUpdate(true); return bean; } @Bean public HibernateTransactionManager transactionManager() { return new HibernateTransactionManager(sessionFactoryBean().getObject()); } }
Code:@Transactional public interface IContractService { void addContract(Contract contract) throws DBException; List<Contract> listContracts(Object hiwiID) throws DBException; void deleteByID(Long contractID) throws DBException; }DAOCode:@Service public class ContractServiceImpl implements IContractService { @Autowired private IContractDAO _contractDAO; public void addContract(Contract contract) throws DBException { _contractDAO.save(contract); } public List<Contract> listContracts(Object hiwiID) throws DBException { return _contractDAO.getByHiwiID(hiwiID); } public void deleteByID(Long contractID) throws DBException { _contractDAO.deleteByID(contractID); } }
Code:public interface IContractDAO { void save(Contract contract) throws DBException; List<Contract> getByHiwiID(Object id) throws DBException; void update(Contract contract) throws DBException; void deleteByID(Serializable contractID) throws DBException; }Code:@Repository public class ContractDAOImpl implements IContractDAO{ @Autowired private SessionFactory sessionFactory; public ContractDAOImpl() { } public void delete(Contract contract) throws DBException { Session s = sessionFactory.getCurrentSession(); s.delete(contract); } public void update(Contract contract) throws DBException { Session s = sessionFactory.getCurrentSession(); s.update(contract); } public void save(Contract contract) throws DBException { Session s = sessionFactory.getCurrentSession(); s.save(contract); } @SuppressWarnings("unchecked") public List<Contract> getByHiwiID(Object id) throws DBException { [...] } }



Reply With Quote
