# Problem mit Spring & Hibernate Sessions



## dunhillone (16. Mrz 2010)

Hi

Habe das Problem leider schon im Java Allgemein Forum gepostet, denke hier passt es aber besser.

Wie auch immer..

Bin jetzt seit längerem an einem Problem mit Spring und Hibernate Sessions.

Hier die Meldung:


```
Exception in thread "main" 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:63)
    at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:622)
    at ch.sisa.reportit.core.persistence.HibernateDataAccessService.getSession(HibernateDataAccessService.java:60)
    at ch.sisa.reportit.core.persistence.HibernateDataAccessService.getReportitObjects(HibernateDataAccessService.java:107)
    at ch.sisa.reportit.core.business.BusinessService.getBusinessObjects(BusinessService.java:111)
    at ch.sisa.reportit.TestSpring.<init>(TestSpring.java:29)
    at ch.sisa.reportit.TestSpring.main(TestSpring.java:20)
```

Hat jemand n Plan?

Hier noch der Code resp. die Config :


```
<?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:oxm="http://www.springframework.org/schema/oxm"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/oxm [url]http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd[/url]
        [url=http://www.springframework.org/schema/beans]Index of /schema/beans[/url] [url]http://www.springframework.org/schema/beans/spring-beans-3.0.xsd[/url]
        [url=http://www.springframework.org/schema/tx]Index of /schema/tx[/url] [url]http://www.springframework.org/schema/tx/spring-tx-3.0.xsd[/url]
        [url=http://www.springframework.org/schema/context]Index of /schema/context[/url] http://www.springframework.org/schema/context/spring-context.xsd">
 
    <context:annotation-config />
 
    <context:component-scan base-package="ch.test.inhere" />
 
    <tx:annotation-driven transaction-manager="transactionManager" mode="aspectj"/>
 
    <bean id="test-ds" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
        <property name="url"
            value="jdbc:jtds:sqlserver://localhost/testdb;instance=SQLEXPRESS" />
        <property name="username" value="xxx" />
        <property name="password" value="xxx" />
    </bean>
 
    <bean id="test-sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
 
        <property name="dataSource" ref="test-ds" />
        <property name="packagesToScan" value="ch.test.inhere" />
 
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
                <prop key="hibernate.default_schema">dbo</prop>
                <prop key="show_sql">true</prop>
            </props>
        </property>
    </bean>
        
     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="test-sessionFactory"/>
    </bean>
</beans>
```


```
private SessionFactory sessionFactory = null;
    
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
 
    @Required
    @Resource(name="test-sessionFactory")
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;   
    }
 
    @Override
    @Transactional(readOnly = true)
    public Object getFromDBObject(Class<?> objectClass, Long id) {
 
 
        
        Object objc = getSession().get(objectClass, id);
        
        objc;
    }
 
    private Session getSession() {
        // TODO Auto-generated method stub
        return sessionFactory.getCurrentSession();
    }
```


----------



## Gast2 (16. Mrz 2010)

schau mal hier rein
http://www.java-forum.org/application-tier/92960-spring-hibernate-exception.html
oder hier
http://www.java-forum.org/application-tier/90335-transaktionen-spring-2.html


----------



## dunhillone (16. Mrz 2010)

Hab das Problem mitlerweile gelöst.

Letzendlich lag es daran, dass ich die AspectJ jars nicht im Classpath hatte :-/


----------

