views:

173

answers:

1

I am using JPA with openjpa implementation beneath, on a Geronimo application server. I am also using MySQL database. I have a problem with updating object with nullable Date property. When I'm trying to merge entity with Date property set to null, no sql update script is generated (or when other fields are modified, sql update script is generated, but date field is ommited from it). If date field is set to some other not null value, update script is properly generated.

Did anyone have problem like that?

A: 

OpenJPA makes certain assumptions when you've detached (and presumably serialized) an entity and then merge it back in.

It's usually the serialization that kicks in this kind of problem - when the entity is serialized OpenJPA loses its StateManager which tracks which fields were loaded. As a result when you merge the entity back in with a null value OpenJPA isn't certain that the field was ever loaded and thinks it hasn't been changed.

There are a couple of options to fix this :

You can configure OpenJPA to use a serializable StateManager - and it will keep track of which fields you've loaded. To do this add the following property to persistence.xml.

<property name="openjpa.DetachState" value="loaded(DetachedStateField=true)"/>

Or tell OpenJPA to load a set of fields before the entity is detached. OpenJPA will then know which fields were present and will handle a null value properly. Your options are to load the fetch-groups (an OpenJPA concept, but by default it loads all non-LAZY fields), or every field (this can be expensive).

I'd recommend fetch-groups in most cases, here's the property for persistence.xml.

<property name="openjpa.DetachState" value="fetch-groups"/>

You can do some clever things with detached object graphs if you're so inclined. The OpenJPA manual has more information at http://openjpa.apache.org/builds/1.2.2/apache-openjpa-1.2.2/docs/manual/manual.html#ref_guide_detach_graph

Mike
Thanks. This solved problem.
Vladimir