Announcement Announcement Module
Collapse
No announcement yet.
could not initialize proxy - the owning Session was closed Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • could not initialize proxy - the owning Session was closed

    Hi
    I am new in spring and hibernate, every thing works fine until I start with one-to-one relation, my unit tests start to failed, i think that I need change something in *.hbm.xml file but I try different options but didn't get satisfied result. Here is my error message during performing unit test:
    Code:
    2006-08-11 19:40:26 org.hibernate.LazyInitializationException <init>
    SEVERE: could not initialize proxy - the owning Session was closed
    org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
    	at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:53)
    	at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:84)
    	at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:134)
    	at org.noip.jorg.pms.manager.AddressImpl$$EnhancerByCGLIB$$30e7c4a8.toString(<generated>)
    	at org.noip.jorg.pms.manager.AddressImplTest.testDelete(AddressImplTest.java:87)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    My address.java
    Code:
    @Configurable("templateAddress")
    public class AddressImpl implements Address {
        private Integer id;
        private String city;
        private String state;
        private String zip;
        private String country;
        private String phone;
        private String fax;
        private static AddressDao dao;
        private Member member;
        
        public void save(){
            dao.save(this);
        }
        
        public void delete(){
            dao.delete(this);
        }
    
        public AddressDao getDao() {
            return dao;
        }
    
        public void setDao(AddressDao dao) {
            this.dao = dao;
        }
    //setters and getters
        
    }
    My Address.hbm.xml

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sf.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
    	<class name="org.noip.jorg.pms.manager.AddressImpl" table="addresses" lazy="true">
    		<id name="id" column="address_id" type="java.lang.Integer" unsaved-value="0">
    			<generator class="native"></generator>
    		</id>
    		<property name="city" column="city" type="java.lang.String"></property>
    		<property name="state" column="state" type="java.lang.String"></property>
    		<property name="zip" column="zip" type="java.lang.String"></property>
    		<property name="country" column="country" type="java.lang.String"></property>
    		<property name="phone" column="phone" type="java.lang.String"></property>
    		<property name="fax" column="fax" type="java.lang.String"></property>
    		<one-to-one name="member" class="org.noip.jorg.pms.manager.MemberImpl" property-ref="address"></one-to-one>
    		<one-to-one name="customer" class="org.noip.jorg.pms.manager.CustomerImpl" property-ref="address"></one-to-one>
    	</class>
    </hibernate-mapping>
    My MemberImpl.java

    Code:
    @Configurable("templateMember")
    public class MemberImpl implements Member {
        private Integer id;
        private Contact contact;
        private Address address;
        private static MemberDao dao;
        private Set<Task> tasks = new TreeSet<Task>();
        private Set<Group> groups = new HashSet<Group>();
        
        public void addToGroup(Group group){
            if(group == null)
                throw new InvalidAttributeValueException("Group can't be null");
            if(groups.contains(group))
                throw new InvalidAttributeValueException("User already belongs to this group");
            this.groups.add(group);
        }
        
        public void removeFromGroup(Group group){
            if(group == null)
                throw new InvalidAttributeValueException("Null group!!");
            if(!groups.contains(group))
                throw new InvalidAttributeValueException("Member don't belong to this group!!");
            this.groups.remove(group);
        }
        
        public void save(){
            dao.save(this);
        }
        
        public void delete(){
            dao.delete(this);
        }
    
        public  MemberDao getDao() {
            return dao;
        }
    
        public void setDao(MemberDao dao) {
            this.dao = dao;
        }
    
        public Address getAddress() {
            return address;
        }
    
        public void setAddress(Address address) {
            this.address = address;
        }
    
        public Contact getContact() {
            return contact;
        }
    
        public void setContact(Contact contact) {
            this.contact = contact;
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public Set<Task> getTasks() {
            return tasks;
        }
    
        public void setTasks(Set<Task> tasks) {
            this.tasks = tasks;
        }
    
        public Set<Group> getGroups() {
            return groups;
        }
    
        public void setGroups(Set<Group> groups) {
            this.groups = groups;
        }
       
    }

    [B} MemberImpl.hbm.xml
    [/B]
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sf.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
    	<class name="org.noip.jorg.pms.manager.MemberImpl" table="members">
    		<id name="id" column="member_id" type="java.lang.Integer" unsaved-value="0">
    			<generator class="native"></generator>
    		</id>
    		<set name="groups" table="members_groups">
    			<key column="member_id"></key>
    			<many-to-many column="group_id" class="org.noip.jorg.pms.manager.GroupImpl"></many-to-many>
    		</set>
    		<many-to-one name="address" class="org.noip.jorg.pms.manager.AddressImpl" column="address_id" unique="true" cascade="save-update"></many-to-one>
    		<many-to-one name="contact" class="org.noip.jorg.pms.manager.ContactImpl" column="contact_id" unique="true" cascade="save-update"></many-to-one>
    		<set name="tasks" table="members_tasks">
    			<key column="member_id"></key>
    			<many-to-many column="task_id" class="org.noip.jorg.pms.project.Task"></many-to-many>
    		</set>
    	</class>
    </hibernate-mapping>
    AddressImplTest.java where I get error message

    Code:
    package org.noip.jorg.pms.manager;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import junit.framework.TestCase;
    
    public class AddressImplTest extends TestCase {
        public static ClassPathXmlApplicationContext appContext = null;
        private Address address, address1, address2;
        private Members members;
        private Member member, member1, member2;
        
        protected void setUp() throws Exception {
            if (appContext == null)
                appContext = new ClassPathXmlApplicationContext("dataAccessContext.xml");
           
            
            members = new MembersImpl();
            members.deleteAll();
            
            member = members.create();
            member1 = members.create();
            member2 = members.create();
            
            
            address = new AddressImpl();
            address.setCity("Rydułtowy");
            address.setCountry("Poland");
            address.setFax("032-45678");
            address.setPhone("032-4568-767");
            address.setState("Sląskie");
            address.setZip("44-280");
            address.save();
            
            address1 = new AddressImpl();
            address1.setCity("Rybnik");
            address1.setCountry("Poland");
            address1.setFax("032-49678");
            address1.setPhone("032-4968-767");
            address1.setState("Sląskie");
            address1.setZip("44-220");
            address1.save();
            
            address2 = new AddressImpl();
            address2.setCity("Essen");
            address2.setCountry("Deutschland");
            address2.setFax("0048-49678");
            address2.setPhone("0048-4968-767");
            address2.setState("Essen Ost");
            address2.setZip("22-333");
            address2.save();
            
            
            
            member.setAddress(address);
            address.setMember(member);
            address.save();
            member.save();
            
            member1.setAddress(address1);
            address1.setMember(member1);
            address1.save();
            member1.save();
            
            member2.setAddress(address2);
            address2.setMember(member2);
            address2.save();
            member2.save();
           
    
        }
    
        /*
         * Test method for 'org.noip.jorg.pms.manager.AddressImpl.save()'
         */
        public void testSave() {
            assertEquals(member.getAddress(), members.find(member.getId()).getAddress());
            assertEquals(member1.getAddress(), members.find(member1.getId()).getAddress());
            assertEquals(member2.getAddress(), members.find(member2.getId()).getAddress());
        }
    
        /*
         * Test method for 'orgname.noip.jorg.pms.manager.AddressImpl.delete()'
         */
        public void testDelete() {
            address.delete();
            assertEquals(null, members.find(member.getId()).getAddress());
            address1.delete();
            assertEquals(null, members.find(member1.getId()).getAddress());
            address2.delete();
            assertEquals(null, members.find(member2.getId()).getAddress());
        }
    
    }
    this code is a part of project management system for student organization develop by students JORG, i will be thankfully for any response

  • #2
    You have a classic case of LIE - Lazy Initialization error. This topic has been discussed so many times that I suggest to do a search on the forum and point to the hibernate documentation. Basically, you are using lazy relationships that require an open session (so the lazy loading can happen). Inside Spring you can use OpenSessionInViewFilter/Interceptor or the HibernateInterceptor to keep a Hibernate session open during a request or method calls.

    Comment


    • #3
      solution

      Hi thanks, I also find such solution http://today.java.net/pub/a/today/20...html?page=last

      Comment


      • #4
        lazy=&quot;false&quot;

        Hi lipa,

        I agree with Costin. However, if you want a quick-and-dirty fix that you can improve later but that allows you to continue in the short-term without worrying about lazy initialization exceptions, true using the lazy="false" attribute in your Hibernate mapping files. Here is an example (notice the lazy="false" attribute in the set property):

        Code:
        <?xml version="1.0"?>
        <!DOCTYPE hibernate-mapping PUBLIC 
        	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
        
        <hibernate-mapping>
        
          <class name="org.spring.forum.domain.Team" >
        
            <id name="identifier">
              <generator class="sequence"/>
            </id>
        
            <property name="name" type="string" column="NAME" />
        
            <set name="players" inverse="true" lazy="false">
              <key column="TEAM_ID"/>
              <one-to-many class="org.spring.forum.domain.Player"/>
            </set>
        
        </class>
        	
        </hibernate-mapping>
        This is not recommended because you most likely want to lazily load objects from the database so that you don't need to make unnecessary database calls when the data is not needed. However, when I started using Hibernate, I at least wanted to get something working quickly so I could take my time to actually use a standard solution (e.g. OpenSessionInViewFilter, HibernateInterceptor).

        -Arthur Loder

        Comment


        • #5
          See the AbstractTransactional* tests from the test package - they already provide such functionality but wrapping each test inside a transaction which is rolled back at the end of the test (no matter the outcome).
          Take a look at the reference docs for more information. Using such tests is not just easier for doing integration testing but will also bind a session to the thread (for the ongoing transaction) which avoid the lazy problem you encounter. Again if you search the forum you'll find more information.

          Comment

          Working...
          X