如何防止JPA将嵌入对象设置为null,因为它的所有字段都为空? [英] How can I prevent JPA from setting an embeddable object to null just because all its fields are null?

查看:549
本文介绍了如何防止JPA将嵌入对象设置为null,因为它的所有字段都为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管违反直觉并且JPA标准显然不要求,但Eclipselink和Hibernate都竭尽全力去创建NullPointerExceptions的以下来源:当嵌入在实体中的对象的所有字段都为空时,它们将由字段本身替换为空值。这是一个简化的例子:

  @Embeddable 
public class Period {
private开始日期;
public Date getStart(){
return start;
}
私人日期结束;
public Date getEnd(){
return end;
}
public boolean equals(Period other){// TODO:实现hashCode()!
返回Objects.equals(start,other.start)&& Objects.equals(end,other.end);



@Entity
公共类PeriodOwner {
@Embedded @AttributeOverrides({...})
私有期间期间;
public Period getPeriod(){
return period;




$ b $ p
$ b

这个想法是我们可以写一些东西像 po1.getPeriod()。等于(po2.getPeriod())。当直接访问 Date 字段时,我们通过额外的间接程度来支付此费用: po1.getPeriod()。getStart()。这通常是一种很好的折衷,但是当我们不得不写 po1.getPeriod()== null时呢? null:po1.getPeriod()。getStart()代替,因为我们的JPA提供者喜欢null。



我们如何确保 getPeriod()永远不会返回null?不幸的是,在标准的JPA中,既没有Java注释也没有XML设置来解决全局或特定嵌入或嵌入问题。 > JPA规范完全忽略了对null的嵌入对象的处理,并将它留给实现来实现他们的感觉(很好,是的?)。 JPA 2.2+已被要求,但谁知道Oracle是否会打扰提供该功能。



DataNucleus JPA为嵌入式字段/属性提供了2个扩展属性

  @Extension(key =null-indicator-column,value =MY_COL)
@Extension(key =null-indicator-value,value =SomeValue)

等等,当嵌入对象为null时,这个列被设置为这个值,同样当读入对象时可以检测到一个空的嵌入对象并将其正确返回给用户。


Although counterintuitive and apparently not required by the JPA standard, both Eclipselink and Hibernate go to great lengths to create the following source of NullPointerExceptions: When all fields of an object embedded in an entity are null, then they replace the field itself by null. Here is a simplified example:

@Embeddable
public class Period {
    private Date start;
    public Date getStart() {
        return start;
    }
    private Date end;
    public Date getEnd() {
        return end;
    }
    public boolean equals(Period other) { // TODO: implement hashCode()!
        return Objects.equals(start, other.start) && Objects.equals(end, other.end);
    }
}

@Entity
public class PeriodOwner {
    @Embedded @AttributeOverrides({...})
    private Period period;
    public Period getPeriod() {
        return period;
    }
}

The idea is that we can write things like po1.getPeriod().equals(po2.getPeriod()). We pay for this by an additional degree of indirection when accessing the Date fields directly: po1.getPeriod().getStart(). This is often a good trade-off, but not when we have to write po1.getPeriod() == null ? null : po1.getPeriod().getStart() instead because our JPA provider loves null.

How can we make sure getPeriod() never returns null? Unfortunately, in standard JPA there exist neither Java annotations nor XML settings to solve this problem either globally or for specific embeddings or embeddables.

解决方案

The JPA spec totally ignores handling of null embedded objects and leaves it up to implementations to do what they feel like (nice, yes?). It has been requested for JPA 2.2+ but who knows if Oracle will ever bother to provide that.

DataNucleus JPA provides 2 extension properties for an embedded field/property

@Extension(key="null-indicator-column", value="MY_COL")
@Extension(key="null-indicator-value", value="SomeValue")

and so when the embedded object is null then this column is set to this value, and likewise when reading in objects it can detect a null embedded object and return it correctly to the user.

这篇关于如何防止JPA将嵌入对象设置为null,因为它的所有字段都为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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