Announcement Announcement Module
Collapse
No announcement yet.
org.hibernate.LazyInitializationException: failed to lazily initialize a collection o Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • org.hibernate.LazyInitializationException: failed to lazily initialize a collection o

    I don't whats exactly prob is all about but it is throwing me following exception.

    From other thread i understood of using getSession().load(UserDto.class, new String(id)); or Hibernate.initialize(ref to collection); but still im facing same exception.

    Please let me know is that i'm correct.

    Code:
    [ERROR] 18:03:14 LazyInitializationException - failed to lazily initialize a collection of role: com.fulcrum.authentication.dto.UserDto.roles, no session or session was closed
    org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.fulcrum.authentication.dto.UserDto.roles, no session or session was closed
    	at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
    	at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
    	at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
    	at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
    	at org.hibernate.collection.PersistentSet.toString(PersistentSet.java:309)
    	at com.fulcrum.services.UserServiceImpl.findById(UserServiceImpl.java:37)
    	at com.fulcrum.services.AuthenticationServiceImpl.authenticate(AuthenticationServiceImpl.java:22)
    	at com.fulcrum.authentication.controller.LoginController.execute(LoginController.java:35)
    	at com.fulcrum.base.controller.BaseSimpleFormController.onSubmit(BaseSimpleFormController.java:36)
    	at org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:267)
    Here is my Java code using HibernateTemplate
    Code:
    	public UserDto findById(final String id) throws BusinessException {
    		logger.info("Loading user: "+id);
    		return (UserDto) getHibernateTemplate().get(UserDto.class, new String(id));
    	}
    and HBM file
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping>
    
        <class name="com.fulcrum.authentication.dto.UserDto" table="FL_USER">
    
    		<id name="username" type="string" unsaved-value="null" >
    			<column name="USER_ID" sql-type="varchar2(2000)" not-null="true"/>
    			<generator class="assigned"/>
    		</id>		
    		
    		<property name="firstName" type="string" update="true" insert="false" unique="false"></property>
    		<property name="lastName" type="string" update="true" insert="false" unique="false"></property>
    		<property name="active" type="string" update="true" insert="false" unique="false" not-null="true"></property>
    		
    		<set name="roles" table="FL_USER_ROLES" >
    			<key column="USER_ID"/>
    			<many-to-many column="ROLE_ID" lazy="false" unique="true" class="com.fulcrum.authentication.dto.RoleDto" />
    		</set>
    		
        </class>
        
        <class name="com.fulcrum.authentication.dto.RoleDto" table="FL_ROLES">
        	<id name="roleId" type="string" unsaved-value="null">
        		<column name="ROLE_ID" sql-type="varchar2(4000)" not-null="true"/>
        		<generator class="assigned" />
        	</id>
    		<property name="role" column="ROLE" type="string" update="false" insert="false" unique="false"></property>
    		<property name="roleDesc" column="ROLE_DESC" type="string" update="false" insert="false" unique="false"></property>
        </class>
    </hibernate-mapping>

  • #2
    Once the Hibernate Session is closed anything that hasn't been loaded, can't be loaded without reattaching your entity to a Hibernate Session. If you aren't using Transactions, OpenSessionInView or managing the Session lifecycle yourself, then the Session will end once the statement has been executed. Try adding a transaction to your AuthenticationServiceImpl.
    http://static.springframework.org/sp...ransaction.htm

    Comment


    • #3
      Still facing same issue even after following change
      Code:
      	public UserDto findById(final String id) throws BusinessException {
      		UserDto userDto = new UserDto();
      		Transaction tx = getSession().beginTransaction();
      		tx.begin();
      		userDto =(UserDto) getHibernateTemplate().get(UserDto.class, new String(id));
      		logger.info("Loading user: "+id);
      		tx.commit();
      		return userDto;

      Comment


      • #4
        You need to wrap the service method in the transaction as well. Your getting the error in UserServiceImpl.java:37 not in the DAO call. When your UserServiceImpl is trying to access the collection its allready closed.

        Comment


        • #5
          Originally posted by TerpInMD View Post
          You need to wrap the service method in the transaction as well. Your getting the error in UserServiceImpl.java:37 not in the DAO call. When your UserServiceImpl is trying to access the collection its allready closed.
          yaa I tryied with @Transactional and it worked so.. thanks

          Comment


          • #6
            Hi TerpInMD
            As you said we need to wrap the service method in the transaction as well. we are getting the error in UserServiceImpl.java:37 not in the DAO call. When your UserServiceImpl is trying to access the collection its allready closed.

            could you pls explain how we can achive this with a sample code.

            Thanks a lot

            Comment


            • #7
              Have found the right configuration for your problem? I'm facing the same issue, integrating CXF soap binding in front of Spring services using hibernate DAO.

              Comment

              Working...
              X