簡體   English   中英

使@Embeddable類可選?

[英]Make an @Embeddable class optional?

我有一個使用@Embeddable注釋的DateInterval類,它有兩個持久字段startDateendDate DateInterval字段可以是可選的,具體取決於使用它的持久化類。 如果DateInterval字段是可選的,則其startDateendDate都應該可以為空。

如何使用JPA 2和/或Hibernate實現此功能?

如果我直接注釋DateInterval的字段,如下所示,那么所有DateInterval字段都不是可選的,這顯然不是我想要的。

@Embeddable
class DateInterval {
    @Column(nullable = false)
    public Date getStartDate() {
    }
}

我嘗試了以下,但沒有奏效。

class Foo {
@Embedded
@Column(nullable = true)
    public DateInterval getDateInterval() {
    }
}

有什么建議么? 謝謝!

如果要覆蓋默認設置,則應使用@AttributeOverride (僅一列)或@AttributeOverrides(如果多於一列)

請改用

public class Foo {

    private DateInterval dateInterval;

    @Embedded
    @AttributeOverrides({
        @AttributeOverride(name="startDate", column=@Column(nullable=true)),
        @AttributeOverride(name="endDate", column=@Column(nullable=true))
    })
    public DateInterval getDateInterval() { return this.dateInterval; }
    public void setDateInterval(DateInterval dateInterval) { this.dateInterval = dateInterval; }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM