Nullable Date column merge problem

怎甘沉沦 提交于 2019-12-04 12:32:14

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!