You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by coloradoflyer <op...@parallelsw.com> on 2009/07/08 08:43:00 UTC

null values not updating

Hi all,

I'm working on a very simple setting from
openejb(3.1)/openjpa(1.2.1)/Postgres-8.3

I can retrieve a persistent object without problem, but if I attempt to
update a column to null, the value is not set.

The object basically has a min and max (Long) values.

Here is my abbreviated mapping:
Range {
  Long min;
  Long max;

  @Column(name="min", nullable=true, updatable=true, insertable=true)
  Long getMin() { return min;}
  void setMin(Long a) { min=a;}
 
  @Column(name="max", nullable=true, updatable=true, insertable=true)
  Long getMax() { return max;}
  void setMax(Long a) { max=a;}
}

when I run the following code:
...  the object is disconnected and retrieved from probably another entity
manager.
   Range r = ....
   r.setMin(null);
   entityManager.merge(r);

The update does not happen.  But, if I update setMin(1) the update does take
place.

However if I run the following code things seem to behave:
   Range r = ...
   Range newRangeInThisTransaction = entityManager.find(Range.class,
r.getID());
   newRangeInThisTransation.setMin(r.getMin());
   newRangeInThisTransaction.setMax(r.getMax());
   entityManager.merge(newRangeInThisTransaction);

any help would be greatly appreciated...  If the problem is not obvious, I
will work on a small working example to show the problem.

-chris
-- 
View this message in context: http://n2.nabble.com/null-values-not-updating-tp3224059p3224059.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: null values not updating

Posted by Daryl Stultz <da...@6degrees.com>.
On Mon, Jul 20, 2009 at 1:22 AM, coloradoflyer <op...@parallelsw.com>wrote:

>
> Hi Daryl,
>
> I tried using value="loaded(AccessUnloaded=false)", it did not allow me to
> set the values to null..


If your case was more like mine I'd say that's good news. It shouldn't allow
you to set the values to anything, not even read them. I would say you just
need to make sure your fields are loaded. Looking at your test case, it
doesn't look like it's a problem of loaded fields which makes me think I'm
at the end my usefulness in helping you. I guess I would try something like
creating or retrieving on one em, closing the em, setting the value, then
merging on a different em and see what happens.



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

Re: null values not updating

Posted by coloradoflyer <op...@parallelsw.com>.
Hi Daryl,

I tried using value="loaded(AccessUnloaded=false)", it did not allow me to
set the values to null..

I've also found another problem with the initial setting.  I have other
tables in the system, for the most part they work, but I do have a
manyTomany, and I get an IllegalStateException that seems to have come up
since making the fetch-groups addition.
Again, the object **should** be disconnected, I'm not sure why its seems
here to have some sort of state connection.

The exception I'm seeing is:
org.apache.openjpa.persistence.InvalidStateException: The context has been
closed.  The stack trace at which the context was closed is held in the
embedded exception.
FailedObject: java.lang.IllegalStateException
	at org.apache.openjpa.kernel.BrokerImpl.assertOpen(BrokerImpl.java:4370)
	at
org.apache.openjpa.kernel.BrokerImpl.beginOperation(BrokerImpl.java:1766)
	at org.apache.openjpa.kernel.BrokerImpl.isActive(BrokerImpl.java:1736)
	at
org.apache.openjpa.kernel.StateManagerImpl.dirty(StateManagerImpl.java:1567)
	at
org.apache.openjpa.kernel.StateManagerImpl.dirty(StateManagerImpl.java:1515)
	at org.apache.openjpa.util.Proxies.dirty(Proxies.java:66)
	at
org.apache.openjpa.util.ProxyCollections.beforeClear(ProxyCollections.java:151)
	at org.apache.openjpa.util.java$util$ArrayList$proxy.clear(Unknown Source)


Daryl Stultz wrote:
> 
> On Thu, Jul 16, 2009 at 1:30 AM, coloradoflyer
> <op...@parallelsw.com>wrote:
> 
>>
>> Hi Kevin,
>>
>> Thanks for this link, I got things working by setting <property
>> name="openjpa.DetachState" value="fetch-groups"/>.  I dont really
>> understand
>> why I need this, The 'loaded' value seemed like it should have had the
>> proper functionality.
>>
> 
> Chris, what happens when you use this instead:
> <property name="openjpa.DetachState"
> value="loaded(AccessUnloaded=false)"/>
> 
> (I could try it myself since you provide the test, but I figure it would
> be
> easier for you.) I ask because I tried fetch-groups to solve my "not
> writing
> null" problem and it didn't work. It's possible your test may fail on
> trying
> to access an unloaded field.
> 
> Thanks.
> 
> -- 
> Daryl Stultz
> _____________________________________
> 6 Degrees Software and Consulting, Inc.
> http://www.6degrees.com
> mailto:daryl@6degrees.com
> 
> 

-- 
View this message in context: http://n2.nabble.com/null-values-not-updating-tp3224059p3287105.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: null values not updating

Posted by Daryl Stultz <da...@6degrees.com>.
On Thu, Jul 16, 2009 at 1:30 AM, coloradoflyer <op...@parallelsw.com>wrote:

>
> Hi Kevin,
>
> Thanks for this link, I got things working by setting <property
> name="openjpa.DetachState" value="fetch-groups"/>.  I dont really
> understand
> why I need this, The 'loaded' value seemed like it should have had the
> proper functionality.
>

Chris, what happens when you use this instead:
<property name="openjpa.DetachState" value="loaded(AccessUnloaded=false)"/>

(I could try it myself since you provide the test, but I figure it would be
easier for you.) I ask because I tried fetch-groups to solve my "not writing
null" problem and it didn't work. It's possible your test may fail on trying
to access an unloaded field.

Thanks.

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

Re: null values not updating

Posted by coloradoflyer <op...@parallelsw.com>.
Hi Kevin,

Thanks for this link, I got things working by setting <property
name="openjpa.DetachState" value="fetch-groups"/>.  I dont really understand
why I need this, The 'loaded' value seemed like it should have had the
proper functionality.

thanks again,
-chris


Kevin Sutter wrote:
> 
> Detecting null fields during re-attachment is tricky.  How do you know if
> the field was null'd out explicitly, or if it was never filled in in the
> first place?  OpenJPA has attempted to provide the facilities for
> detecting
> these conditions [1].  In the referenced section, it discusses how you can
> configure your application to do the "proper" amount of detection.  As you
> will read, there may be some performance concerns with some of the
> processing, so you have to evaluate your application's requirements before
> just blindly turning these options on or off.
> 
> [1]
> http://openjpa.apache.org/builds/1.2.1/apache-openjpa-1.2.1/docs/manual/manual.html#ref_guide_detach
> 
> On Wed, Jul 8, 2009 at 11:24 AM, Daryl Stultz <da...@6degrees.com> wrote:
> 
>> On Wed, Jul 8, 2009 at 9:05 AM, Rick Curtis <cu...@gmail.com> wrote:
>>
>> > Chris-
>> >
>> > You may want to try adding a @Version field to your Entity.
>> >
>>
>> I've overcome the problem without any @Version annotations (and no
>> LockManager). I'm still interested in the cause of the Chris's problem.
>> As
>> well, an explanation of why @Version would fix things would be helpful.
>>
>> --
>> Daryl Stultz
>> _____________________________________
>> 6 Degrees Software and Consulting, Inc.
>> http://www.6degrees.com
>> mailto:daryl@6degrees.com
>>
> 
> 

-- 
View this message in context: http://n2.nabble.com/null-values-not-updating-tp3224059p3267455.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: null values not updating

Posted by mjdenham <mj...@gmail.com>.
I hit this problem a couple of months ago and discovered it happens if you
serialize the object because the serialize/deserialize process seems to lose
track of null values.  There is an openjpa setting which fixes this but my
problem was that I mistakenly had a remote interface to my ejb instead of a
local interface (simply @Remote instead of @Local) and the remote interface
was causing the serialization to occur.  When I switched to a local
interface there was no serialization of the persistent objects and the null
value was correctly saved.

So, in brief, it is worth checking first of all that you are not
accidentally serializing the persistent objects and if you need to serialize
them there is a setting which will make it all work correctly - I would have
to dig around to find the setting.

Regards
Martin
-- 
View this message in context: http://n2.nabble.com/null-values-not-updating-tp3224059p3232848.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: null values not updating

Posted by Daryl Stultz <da...@6degrees.com>.
On Wed, Jul 8, 2009 at 12:51 PM, Kevin Sutter <kw...@gmail.com> wrote:

> Detecting null fields during re-attachment is tricky.  How do you know if
> the field was null'd out explicitly, or if it was never filled in in the
> first place?


That reminds me, I set this:
<property name="openjpa.DetachState" value="loaded(AccessUnloaded=false)"/>

so I don't get null when accessing an unloaded field (it throws exception).

Also, while I don't believe this is related to Chris's situation (since he
stated setting the field to some other non-null value works) I spent an
awful lot of time trying to figure out why a changed property was not being
updated in the DB: if you set a property while an entity is attached to an
em, then you detach from the em, reattach to another em, the merged changes
will not be saved. Detach from first em before setting properties.

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

Re: null values not updating

Posted by Kevin Sutter <kw...@gmail.com>.
Detecting null fields during re-attachment is tricky.  How do you know if
the field was null'd out explicitly, or if it was never filled in in the
first place?  OpenJPA has attempted to provide the facilities for detecting
these conditions [1].  In the referenced section, it discusses how you can
configure your application to do the "proper" amount of detection.  As you
will read, there may be some performance concerns with some of the
processing, so you have to evaluate your application's requirements before
just blindly turning these options on or off.

[1]
http://openjpa.apache.org/builds/1.2.1/apache-openjpa-1.2.1/docs/manual/manual.html#ref_guide_detach

On Wed, Jul 8, 2009 at 11:24 AM, Daryl Stultz <da...@6degrees.com> wrote:

> On Wed, Jul 8, 2009 at 9:05 AM, Rick Curtis <cu...@gmail.com> wrote:
>
> > Chris-
> >
> > You may want to try adding a @Version field to your Entity.
> >
>
> I've overcome the problem without any @Version annotations (and no
> LockManager). I'm still interested in the cause of the Chris's problem. As
> well, an explanation of why @Version would fix things would be helpful.
>
> --
> Daryl Stultz
> _____________________________________
> 6 Degrees Software and Consulting, Inc.
> http://www.6degrees.com
> mailto:daryl@6degrees.com
>

Re: null values not updating

Posted by Daryl Stultz <da...@6degrees.com>.
On Wed, Jul 8, 2009 at 9:05 AM, Rick Curtis <cu...@gmail.com> wrote:

> Chris-
>
> You may want to try adding a @Version field to your Entity.
>

I've overcome the problem without any @Version annotations (and no
LockManager). I'm still interested in the cause of the Chris's problem. As
well, an explanation of why @Version would fix things would be helpful.

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

Re: null values not updating

Posted by Rick Curtis <cu...@gmail.com>.
Chris-

You may want to try adding a @Version field to your Entity.

-Rick

On Wed, Jul 8, 2009 at 1:43 AM, coloradoflyer <op...@parallelsw.com>wrote:

>
> Hi all,
>
> I'm working on a very simple setting from
> openejb(3.1)/openjpa(1.2.1)/Postgres-8.3
>
> I can retrieve a persistent object without problem, but if I attempt to
> update a column to null, the value is not set.
>
> The object basically has a min and max (Long) values.
>
> Here is my abbreviated mapping:
> Range {
>  Long min;
>  Long max;
>
>  @Column(name="min", nullable=true, updatable=true, insertable=true)
>  Long getMin() { return min;}
>  void setMin(Long a) { min=a;}
>
>  @Column(name="max", nullable=true, updatable=true, insertable=true)
>  Long getMax() { return max;}
>  void setMax(Long a) { max=a;}
> }
>
> when I run the following code:
> ...  the object is disconnected and retrieved from probably another entity
> manager.
>   Range r = ....
>   r.setMin(null);
>   entityManager.merge(r);
>
> The update does not happen.  But, if I update setMin(1) the update does
> take
> place.
>
> However if I run the following code things seem to behave:
>   Range r = ...
>   Range newRangeInThisTransaction = entityManager.find(Range.class,
> r.getID());
>   newRangeInThisTransation.setMin(r.getMin());
>   newRangeInThisTransaction.setMax(r.getMax());
>   entityManager.merge(newRangeInThisTransaction);
>
> any help would be greatly appreciated...  If the problem is not obvious, I
> will work on a small working example to show the problem.
>
> -chris
> --
> View this message in context:
> http://n2.nabble.com/null-values-not-updating-tp3224059p3224059.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>

Re: null values not updating

Posted by coloradoflyer <op...@parallelsw.com>.
I've made the unit test as small as I possibly could think where it would
still make sense.  I tried this out in hsql and got the same results, so the
config's have the postgres values commented out and this should use hsql for
its database.  
I also noticed that openejb-3.1 ships with openjpa-1.1 so I just used all of
the libraries shipped with openejb-3.1 with the same result.

The example has 4 basic java files, and the test case.  There is the EJB
Interface (I called it a DAO), and the implementation for it.  There is also
the Entity interface and implementation.  Then the test case which, if it
passes the problem does not exist.

src/com/openrangetest/daos/AgeRangeDAO.java
src/com/openrangetest/daos/impl/AgeRangeDAOImpl.java

src/com/openrangetest/entities/Range.java
src/com/openrangetest/entities/impl/AgeRangeImpl.java

src/com/openrangetest/tests/OpenEJBTestCase.java

please let me know if you see anything in here that looks wrong.  I am
thinking the changes will be needed somewhere in the conf/openejb.xml file
or in the persistence.xml file...

thanks in advance,
-chris

Daryl Stultz wrote:
> 
> On Sat, Jul 11, 2009 at 2:05 AM, coloradoflyer
> <op...@parallelsw.com>wrote:
> 
>>
>> Hi Daryl,
>>
>> I added in a 'version' colum to the table, and get/set methods for
>> Version
>> and annotated it @Version
>> ...
>> With these changes, the problem did not change.
> 
> 
> Given that I solved my null problems without using @Version, I'm not
> surprised. Can you provide a unit test that exposes the problem? I don't
> think we need to be able to run it, but show enough detail to see what's
> happening. I'm no OpenJPA expert but I think the problem might occur be
> before you set the null value.
> 
> -- 
> Daryl Stultz
> _____________________________________
> 6 Degrees Software and Consulting, Inc.
> http://www.6degrees.com
> mailto:daryl@6degrees.com
> 
> 
http://n2.nabble.com/file/n3267404/rangetest.jar rangetest.jar 
-- 
View this message in context: http://n2.nabble.com/null-values-not-updating-tp3224059p3267404.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: null values not updating

Posted by Daryl Stultz <da...@6degrees.com>.
On Sat, Jul 11, 2009 at 2:05 AM, coloradoflyer <op...@parallelsw.com>wrote:

>
> Hi Daryl,
>
> I added in a 'version' colum to the table, and get/set methods for Version
> and annotated it @Version
> ...
> With these changes, the problem did not change.


Given that I solved my null problems without using @Version, I'm not
surprised. Can you provide a unit test that exposes the problem? I don't
think we need to be able to run it, but show enough detail to see what's
happening. I'm no OpenJPA expert but I think the problem might occur be
before you set the null value.

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

Re: null values not updating

Posted by coloradoflyer <op...@parallelsw.com>.
Hi Daryl,

I added in a 'version' colum to the table, and get/set methods for Version
and annotated it @Version
   @Version
   public int getVersion()
   {
      return _version;
   }
   public void setVersion(int version)
   {
      _version = version;
   }
With these changes, the problem did not change.  I got to thinking that the
problem may be because the class I'm in is decorated with @MappedSuperclass. 
But that was not the issue either, when I moved into the subclass I got the
same results. I did notice that the versions were changing as I updated the
entity, but the null values never seemed to get picked up.


Daryl Stultz wrote:
> 
> On Wed, Jul 8, 2009 at 2:43 AM, coloradoflyer
> <op...@parallelsw.com>wrote:
> 
> I have struggled with this problem recently myself. I'm not a JPA expert.
> It
> may be that the problem lies in this line:
> 
>>
>> when I run the following code:
>> ...  the object is disconnected and retrieved from probably another
>> entity
>> manager.
>>   Range r = ....
>>
> 
> I found that if I instantiated an existing entity myself (like from a
> direct
> ResultSet traversal) or I cloned and existing entity I would get this
> problem. The manual states that in this case (I think it was referring to
> deserialization) it can't tell the difference between a null value an an
> "unloaded" field. If you are in fact using an entity manager to get the
> original instance and you don't leave the VM it should have a proper
> detached state. Can you post a simple unit test that shows how you are
> doing
> things?
> 
> -- 
> Daryl Stultz
> _____________________________________
> 6 Degrees Software and Consulting, Inc.
> http://www.6degrees.com
> mailto:daryl@6degrees.com
> 
> 

-- 
View this message in context: http://n2.nabble.com/null-values-not-updating-tp3224059p3241035.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: null values not updating

Posted by Daryl Stultz <da...@6degrees.com>.
On Wed, Jul 8, 2009 at 2:43 AM, coloradoflyer <op...@parallelsw.com>wrote:

I have struggled with this problem recently myself. I'm not a JPA expert. It
may be that the problem lies in this line:

>
> when I run the following code:
> ...  the object is disconnected and retrieved from probably another entity
> manager.
>   Range r = ....
>

I found that if I instantiated an existing entity myself (like from a direct
ResultSet traversal) or I cloned and existing entity I would get this
problem. The manual states that in this case (I think it was referring to
deserialization) it can't tell the difference between a null value an an
"unloaded" field. If you are in fact using an entity manager to get the
original instance and you don't leave the VM it should have a proper
detached state. Can you post a simple unit test that shows how you are doing
things?

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