-->
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.  [ 8 posts ] 
Author Message
 Post subject: Initialized proxy yields uninitialized proxy error
PostPosted: Mon Dec 27, 2004 6:19 pm 
Newbie

Joined: Mon Aug 02, 2004 3:02 pm
Posts: 15
Hello,

I have a Company object which has a many-to-one association to a Contact object (see mapping below). Both are lazily loaded objects. When I attempt to save the Company object, I get the error pasted below that the Contact object is not initialized. However, when I debug and run Hibernate.isInitialized on the the Contact object in the Company object it yields true. Can anyone point me in the right direction of how to fix this? (BTW - I am using Spring but it is just wrapping the actual Hibernate error)

Thanks!

Hibernate version: 2.1.7

Mapping documents: Excerpt from Company object:

Code:
<many-to-one
            name="primaryContact"
            class="foo.domain.Contact"
            cascade="save-update"
            outer-join="auto"
            update="true"
            insert="true"
            access="property"
            foreign-key="FK_PRIMARY_CONTACT_ID"
            column="PRIMARY_CONTACT_ID"
            not-null="true"
        />



Code between sessionFactory.openSession() and session.close(): session.saveOrUpdateCopy(entity);

Full stack trace of any exception that occurs:
Quote:
org.springframework.dao.InvalidDataAccessApiUsageException: object was an uninitialized proxy for: foo.domain.Contact at org.springframework.orm.hibernate.SessionFactoryUtils.convertHibernat
eAccessException(SessionFactoryUtils.java:533) at org.springframework.orm.hibernate.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:234)
at org.springframework.orm.hibernate.HibernateTemplate.execute(Hibernate
Template.java:248)
at org.springframework.orm.hibernate.HibernateTemplate.saveOrUpdateCopy(
HibernateTemplate.java:412)
at foo.dao.impl.hibernate.BaseDAOImpl.saveOrUpdate(BaseDAOImpl.
java:60)
at foo.service.impl.CompanyServiceImpl.saveOrUpdateCompany(Comp
anyServiceImpl.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)


Name and version of the database you are using:PostgreSQL 8.0 RC2

Debug level Hibernate log excerpt:

Quote:
[DEBUG] 16:48:38 Cascades - processing cascades for: foo.domain.Company

[DEBUG] 16:48:38 Cascades - cascading to collection: foo.domain.Company.children
[DEBUG] 16:48:38 Cascades - cascading to copy()
[DEBUG] 16:48:38 Cascades - version unsaved-value strategy NULL
[DEBUG] 16:48:38 SessionImpl - loading [foo.domain.Contact#1]


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 01, 2005 8:58 pm 
Newbie

Joined: Mon Aug 02, 2004 3:02 pm
Posts: 15
Can someone please help me with this issue? I even explicitly queried the relationship using the fetch keyword and I still get this problem. The issue seems to occur within the Hibernate code itself, since when Hibernate gets the objects and does the dirty check, it is getting the proxy (since the COntact object is lazy).

Thanks,

Rexxe


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 01, 2005 11:36 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
"org.springframework.dao.InvalidDataAccessApiUsageException: object was an uninitialized proxy for: foo.domain.Contact"

This is not a Hibernate exception, I don't know what it means.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 02, 2005 12:20 am 
Newbie

Joined: Mon Aug 02, 2004 3:02 pm
Posts: 15
Thanks for replying Christian.

The exception is actually a Hibernate exception wrapped in a Spring exception. Here is the actual Hibernate exception:

net.sf.hibernate.PersistentObjectException: object was an uninitialized proxy for: foo.domain.Contact

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 02, 2005 4:55 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
please try to do this as simple as possible - like with standard hibernate api and see if it works there.

secondkt you dont show code so hard to see what have caused it

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 02, 2005 12:53 pm 
Newbie

Joined: Mon Aug 02, 2004 3:02 pm
Posts: 15
I changed the code to use standard hibernate API code and I still get the error. Here is the code I used:

Code:
Company returnValue = (Company)session.saveOrUpdateCopy(company);
session.flush();
session.connection().commit();
session.close();


I debugged what was happening in SessionImpl, in particular, in doCopy(Object object, Serializable id, Map copiedAlready) during the cascade.

1. The object starts out as a Contact proxy, but line 4045 of SessionImpl (li.getImplementation()) converts it to its true implementation of a Contact object.

2. Since the Contact object is not new (I'm updating it), it goes to line 4067 where the code gets the result by id and class.

3. Line 4073 of SessionImpl, where the code attempts to unproxy the result from step 2 is where the exception occurs. Since the result loaded in step 2 is a proxy for Contact (since Contact is loaded lazily) and since the get in step 2 does not touch the proxy or uninitialize it, the code is unable to unproxy it which yields the exception. The actual exception occurs at line 996 of SessionImpl in the unproxy method.

It doesn't seem to matter whether I put a proxy or the true implementation of the Contact object in the Company object when I save. The only way I can get this to work is by either making Contact not lazy or by forcing an outer join in the many-to-one mapping of Company to Contact.

Thanks for your help,

Rexxe


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 02, 2005 2:22 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
ok - you keep showing half the code...where is the code from opening the session to the lines you make ? and where is the exeception ?

...

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 02, 2005 4:49 pm 
Newbie

Joined: Mon Aug 02, 2004 3:02 pm
Posts: 15
Max,

The code I provided exists within a Spring web application, so there is a lot of Spring managed things that are done for me. That said, I wrote a JUnit test case that replicates what is happening in the web application. Consequently, I determined where the problem is occuring. What is happening is that because I am using the Open Session In View Filter in singleSession mode, which closes the session after the view is rendered and then reopens upon submission, Spring binds any changes to the object before I have a chance to call session.lock or session.load. As a result, when the object is passed to hibernate's session for saving, the proxy for the contact is not attached to the session and it bombs.

Thanks for your help in this matter,

Rexxe


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.