Nullable Date column merge problem

醉酒当歌 提交于 2019-12-21 19:47:42

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/2453671/nullable-date-column-merge-problem

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