可空的日期列合并问题 [英] Nullable Date column merge problem

查看:97
本文介绍了可空的日期列合并问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Geronimo应用程序服务器上将JPA与下面的openjpa实现一起使用.我也在使用MySQL数据库.我在更新具有nullable Date属性的对象时遇到问题.当我尝试将日期属性设置为null的实体合并时,不会生成任何SQL更新脚本(或修改其他字段时,会生成SQL更新脚本,但会省略日期字段).如果date字段设置为其他一些非null值,则将正确生成更新脚本.

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.

有人有这样的问题吗?

推荐答案

当您分离(并可能序列化了)实体然后将其合并回内部时,OpenJPA会做出某些假设.

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

通常会引发此类问题的序列化-实体进行序列化时,OpenJPA会丢失其StateManager来跟踪加载了哪些字段.结果,当您使用空值将实体合并回时,OpenJPA不确定该字段是否曾经被加载过,并认为它没有被更改.

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 :

您可以将OpenJPA配置为使用可序列化的StateManager-它将跟踪您已加载的字段.为此,将以下属性添加到persistence.xml.

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)"/>

或者告诉OpenJPA在分离实体之前加载一组字段.然后,OpenJPA将知道存在哪些字段,并将正确处理null值.您的选择是加载提取组(OpenJPA概念,但默认情况下加载所有非LAZY字段)或每个字段(这可能很昂贵).

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).

在大多数情况下,我建议使用fetch-groups,这是persistence.xml的属性.

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

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

如果您愿意,可以使用分离的对象图来做一些聪明的事情. OpenJPA手册在

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

这篇关于可空的日期列合并问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆