Announcement Announcement Module
Collapse
No announcement yet.
Simple Hibernate + Spring Injection - Help? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Simple Hibernate + Spring Injection - Help?

    Hi folks, I feel this should be really simple. I'm trying to avoid complexity. I had tried to get JPA working in a Spring managed app using AOP methodology, but it went down a very painful twisty road.

    All I'd like to do is have Hibernate manage my entities, using annotation. The problem I'm having is getting an active HibernateSession to my DAO. I'm hoping I can simply inject a HibernateSession directly, with the annotation factory, but I can't seem to get the right incantation. I'm getting for an error:

    Code:
    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 com.stonekeep.congo.dao.FriendDAO.add(FriendDAO.java:58)
    The applicationContext.xml is definining my datasource (a C3PO pool) and my annotated classes. This should all just work - what am I missing?

    Code:
      <bean class="com.stonekeep.congo.dao.FriendDAO">
    	<property name="dataSource" ref="com.stonekeep.congo.database.Pool"/>
    	<property name="sessionFactory">
    		<ref bean="sessionFactory" />
    	</property>
      </bean>
    
    <!-- Data sources -->
      <bean id="com.stonekeep.congo.database.Pool" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass" value="${database.driver}"/>
        <property name="jdbcUrl" value="${database.url}"/>
        <property name="user" value="${database.user}"/>
        <property name="password" value="${database.password}"/>
        <property name="testConnectionOnCheckout" value="true"/>
      </bean>
      
      <!--  hibernate configuration -->
      <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
      
    	<property name="dataSource">
    		<ref bean="com.stonekeep.congo.database.Pool" />
    	</property>
    	<property name="annotatedClasses">
    		<list>
    			<value>com.stonekeep.congo.data.Friend</value>
    		</list>
    	</property>
    	<property name="hibernateProperties">
    		<props>
    			<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    		
    			<prop key="hibernate.generate_statistics">true</prop>
    			<prop key="hibernate.cache.use_query_cache">true</prop>
    			<prop key="hibernate.connection.release_mode">on_close</prop>
    
    			<prop key="hibernate.show_sql">true</prop>
    			<prop key="hibernate.cglib.use_reflection_optimizer">tru e</prop>
    			<prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
    			<prop key="hibernate.connection.autocommit">false</prop>
    			<prop key="hibernate.jdbc.batch_size">500</prop>
    		</props>
        </property>
      </bean>
    Any help would be appreciated - cookies are available!

  • #2
    Please use the forum search, this question has been answered numerous times before...

    Short answer
    Configure transactions (a transactionamanger AND transactional configuration) without it you will not get transaction.

    Comment


    • #3
      You should search on google to get a best result.





      _________________
      Dịch vụ thiết kế nội thất văn pḥng luôn mang lại cho bạn một không gian nội thất văn pḥng đẹp và sang trọng
      Last edited by ct.nguyen00; Apr 20th, 2011, 02:05 AM.

      Comment

      Working...
      X