-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Spring @Transactional getting: could not initialize proxy...
PostPosted: Sun Sep 14, 2008 4:17 pm 
Newbie

Joined: Tue Oct 09, 2007 5:33 am
Posts: 1
Hibernate version: 3.2.6.ga
Spring version: 2.5.5

This is for standalone application, and I know EXACTLY where the transaction starts and ends. I don't have a problem with reading lazy initialized values outside of transaction (session), I want to read them INSIDE transaction, so please don't send me to read about OpenSessionInView. I want the session to last exactly as long as the transaction lasts.

Files of note:

Spring configuration
Code:
<?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:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">

  <bean id="entityManagerFactory"
     class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
     <property name="jpaVendorAdapter">
        <bean
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
           <property name="showSql" value="true" />
           <property name="generateDdl" value="true" />
           <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
        </bean>
     </property>
     <property name="dataSource"><ref local="dataSource" /></property>
  </bean>

  <bean id="dataSource"
     class="org.apache.commons.dbcp.BasicDataSource"
     destroy-method="close">
        <property name="driverClassName" value="org.gjt.mm.mysql.Driver" />
        <property name="url" value="***" />
        <property name="username" value="***" />
        <property name="password" value="***" />
  </bean>

  <bean id="transactionManager"
     class="org.springframework.orm.jpa.JpaTransactionManager">
     <property name="dataSource"><ref local="dataSource" /></property>
     <property name="entityManagerFactory"><ref local="entityManagerFactory" /></property>
  </bean>
 
  <tx:annotation-driven transaction-manager="transactionManager" />

  <bean
     class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

  <bean id="someService" class="***.SomeServiceImpl" />
 
</beans>


SomeService interface:
Code:
public interface SomeService {
  public void doSomethingInTransaction();
}


SomeServiceImpl class:
Code:
@Transactional
public class SomeServiceImpl implements SomeService {
  private EntityManager entityManager;

  @PersistenceContext(type=PersistenceContextType.TRANSACTION)
  public void setEntityManager(EntityManager entityManager) {
    this.entityManager = entityManager;
  }

  public void doSomethingInTransaction() {
    // Do Something with entityManager that involves retrieval and
    // further read of lazy loaded @Entity beans
  }
}


Nature of the problem/Expected result:

Essentially, I want to start the transaction at the beginning of doSomethingInTransaction(), and end it just after finishing it. The problem is, even though the whole code of the method should run in a transaction I'm getting Caused by: org.hibernate.LazyInitializationException: could not initialize proxy - no Session.

Am I missing something? Do I need to specify somewhere, that the Session object should be bound to the Transaction?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 15, 2008 5:16 am 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
Is the LazyInitializationException thrown from within your method "doSomethingInTransaction()" method ?

_________________
If you found my post helpful, please rate it! Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2009 4:47 pm 
Beginner
Beginner

Joined: Fri Sep 12, 2008 10:55 am
Posts: 33
For my case..which is simliar..yes


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.