You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Daryl Stultz <da...@6degrees.com> on 2009/08/19 21:02:00 UTC

Encountered unmanaged object

Hello,

I'm looking for any clues to help troubleshoot the following error:

Encountered unmanaged object in persistent field
> "blah.model.MyEntity.myPropA" during flush.  However, this field does not
> allow cascade persist. Set the cascade attribute for this field to
> CascadeType.PERSIST or CascadeType.ALL (JPA annotations) or "persist" or
> "all" (JPA orm.xml), or enable cascade-persist globally, or manually persist
> the related field value prior to flushing. You cannot flush unmanaged objects
> or graphs that have persistent associations to unmanaged objects.


It is happening very occasionally on a production system (Tomcat). I can't
reproduce it. Here it is "myPropA" but it happened on "myPropB" as well. In
both cases the objects of A and B were in the database already and not
modified as part of the transaction. Anyone have any idea what could cause
the properties to become "unmanaged"? I have to assume they were managed at
some point since they were retrieved from the database via an entity manager
(and the system is working largely as expected). I could set the cascade to
PERSIST, but I don't need it and I don't know what it will do - maybe it
will try to reinsert a copy of the object already in the database.
Thanks.

OpenJPA 1.2.1

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com

Re: Encountered unmanaged object

Posted by Daryl Stultz <da...@6degrees.com>.
On Wed, Aug 19, 2009 at 8:01 PM, C N Davies <cn...@cndavies.com> wrote:

> will get the exact error you reported. The error never made any sense to me
> because I can't understand how the entity could be detached, because there
> was no entity (for the City).
>

It doesn't quite sound like my case, but causes me to think more about mine.
The error states the entity is "unmanaged" but you describe it as being
"detached". Looking at 8.2 "Entity Lifecycle Management", there is no use of
the word "unmanaged". I'm trying to figure out what JPA considers
"unmanaged". Using the diagram at the bottom, it appears New/Transient,
Removed, and Detached states may all be "Unmanaged" since they aren't, well,
"Managed". The child entity was not Removed or New, at least not as a result
of my actions. I'm pretty sure it was Detached, but since that happens "all
the time" I don't see how that could be the problem. What is "Transient"?
Could my object fall into this state once in a while?

Can anyone tell me exactly what an "Unmanaged" instance is?

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com

RE: Encountered unmanaged object

Posted by C N Davies <cn...@cndavies.com>.
I hadn't set the child entity as not nullable. 

My English is pretty poor but I'll see if I can explain the issue more
clearly. 

I have a list of cities that are stored in an excel file, I pull them in and
for each one I create a City entity. The addresses get pulled in to my
system using Tibco and pre-persist I lookup the city field in the Tibco data
and try to match it against one of my City entities to create the
master/child relationship (Address-->City). Generally this all works fine
but people do mistype the name of the city in the ERP so when the address
data comes in via Tibco I can't always match the city the user typed to any
of my City entities, so the admin gets an alert that the address needs to be
fixed manually. The admin goes in and locates the Address entity in question
and selects the appropriate City entity and saves the Address.

My issue was that because of my JSF and bean code, it was possible to end up
with a nulled City in the Address. Whenever this happens and I call merge I
will get the exact error you reported. The error never made any sense to me
because I can't understand how the entity could be detached, because there
was no entity (for the City). 

Subsequently, because of my fumbling fingers I have created similar
situations during coding and in all cases that I saw this very same error.
Now I can't say that this is your error, but I can say that you will get
this error if you do what I did.

Chris

-----Original Message-----
From: Daryl Stultz [mailto:daryl@6degrees.com] 
Sent: Thursday, 20 August 2009 7:43 AM
To: users@openjpa.apache.org
Subject: Re: Encountered unmanaged object

On Wed, Aug 19, 2009 at 6:18 PM, C N Davies <cn...@cndavies.com> wrote:

>
> I have had this issue quite a number of times, it turned out to be a logic
> issue in the base class for my in my backing bean that would null out the
> child entity in an main entity, when this condition occurs I will always
> get
> this error.


I don't mean to beat this to death, but if the child entity of the main
entity was nulled and the property was declared not nullable, then you
should have gotten a error regarding the null, not "unmanaged entity",
right? And if it was nullable, it should have happily persisted the main
entity. Sorry, still confused, and trying to understand the inner workings
better. It seems to me the child entity is not really null, but it's
(enhanced) state got messed up.

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com


Re: Encountered unmanaged object

Posted by Daryl Stultz <da...@6degrees.com>.
On Wed, Aug 19, 2009 at 6:18 PM, C N Davies <cn...@cndavies.com> wrote:

>
> I have had this issue quite a number of times, it turned out to be a logic
> issue in the base class for my in my backing bean that would null out the
> child entity in an main entity, when this condition occurs I will always
> get
> this error.


I don't mean to beat this to death, but if the child entity of the main
entity was nulled and the property was declared not nullable, then you
should have gotten a error regarding the null, not "unmanaged entity",
right? And if it was nullable, it should have happily persisted the main
entity. Sorry, still confused, and trying to understand the inner workings
better. It seems to me the child entity is not really null, but it's
(enhanced) state got messed up.

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com

RE: Encountered unmanaged object

Posted by C N Davies <cn...@cndavies.com>.
Hi Darryl,

Well I am not a JPA expert so I'll answer the bit I can.

"How did you end up resolving your problem? I assume the City child is not
allowed to be null? I'm not clear how your system got into the state it was
in (is it repeatable?)."

I have had this issue quite a number of times, it turned out to be a logic
issue in the base class for my in my backing bean that would null out the
child entity in an main entity, when this condition occurs I will always get
this error. Can I reproduce it? ... more times than I care to count :) 

Chris
 
-----Original Message-----
From: Daryl Stultz [mailto:daryl@6degrees.com] 
Sent: Thursday, 20 August 2009 4:48 AM
To: users@openjpa.apache.org
Subject: Re: Encountered unmanaged object

On Wed, Aug 19, 2009 at 4:27 PM, C N Davies <cn...@cndavies.com> wrote:

> Hi Daryl,
>
> I get that exact error if one of my fields in my main entity has a
> reference
> to a child entity and the child is null.


The next line of the error message is something like this:

> FailedObject: blah.model.A-1234


I think that means the instance of A with id 1234 is the object that's
failing, thus it's not null. Or maybe the instance of A has a null property?
Just trying to see if my case fits with yours or JIRA OPENJPA-714.

Can an Open JPA expert tell me if the problem is with the state of MyEntity,
A or some property of A?

For example if I have an Address
> entity that contains a City entity and for some reason my City entity is
> null, then when I try to persist the Address entity, I'll get this error.
> It
> only seems to happen when I have the child entity cascade set to REFRESH.


Hmm, I'm currently using CascadeType "NONE" (empty) for the properties that
have failed.

How did you end up resolving your problem? I assume the City child is not
allowed to be null? I'm not clear how your system got into the state it was
in (is it repeatable?).

Thanks for the input.
-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com


Re: Encountered unmanaged object

Posted by Daryl Stultz <da...@6degrees.com>.
On Wed, Aug 19, 2009 at 4:27 PM, C N Davies <cn...@cndavies.com> wrote:

> Hi Daryl,
>
> I get that exact error if one of my fields in my main entity has a
> reference
> to a child entity and the child is null.


The next line of the error message is something like this:

> FailedObject: blah.model.A-1234


I think that means the instance of A with id 1234 is the object that's
failing, thus it's not null. Or maybe the instance of A has a null property?
Just trying to see if my case fits with yours or JIRA OPENJPA-714.

Can an Open JPA expert tell me if the problem is with the state of MyEntity,
A or some property of A?

For example if I have an Address
> entity that contains a City entity and for some reason my City entity is
> null, then when I try to persist the Address entity, I'll get this error.
> It
> only seems to happen when I have the child entity cascade set to REFRESH.


Hmm, I'm currently using CascadeType "NONE" (empty) for the properties that
have failed.

How did you end up resolving your problem? I assume the City child is not
allowed to be null? I'm not clear how your system got into the state it was
in (is it repeatable?).

Thanks for the input.
-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com

RE: Encountered unmanaged object

Posted by C N Davies <cn...@cndavies.com>.
Hi Daryl,

I get that exact error if one of my fields in my main entity has a reference
to a child entity and the child is null. For example if I have an Address
entity that contains a City entity and for some reason my City entity is
null, then when I try to persist the Address entity, I'll get this error. It
only seems to happen when I have the child entity cascade set to REFRESH.
Took me days to work this one out but eventually tracked it down.

Hth 

Chris

-----Original Message-----
From: Daryl Stultz [mailto:daryl@6degrees.com] 
Sent: Thursday, 20 August 2009 3:02 AM
To: OpenJPA User List
Subject: Encountered unmanaged object

Hello,

I'm looking for any clues to help troubleshoot the following error:

Encountered unmanaged object in persistent field
> "blah.model.MyEntity.myPropA" during flush.  However, this field does not
> allow cascade persist. Set the cascade attribute for this field to
> CascadeType.PERSIST or CascadeType.ALL (JPA annotations) or "persist" or
> "all" (JPA orm.xml), or enable cascade-persist globally, or manually
persist
> the related field value prior to flushing. You cannot flush unmanaged
objects
> or graphs that have persistent associations to unmanaged objects.


It is happening very occasionally on a production system (Tomcat). I can't
reproduce it. Here it is "myPropA" but it happened on "myPropB" as well. In
both cases the objects of A and B were in the database already and not
modified as part of the transaction. Anyone have any idea what could cause
the properties to become "unmanaged"? I have to assume they were managed at
some point since they were retrieved from the database via an entity manager
(and the system is working largely as expected). I could set the cascade to
PERSIST, but I don't need it and I don't know what it will do - maybe it
will try to reinsert a copy of the object already in the database.
Thanks.

OpenJPA 1.2.1

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com