【问题标题】:@ElementCollection of Enum in Embeddable可嵌入枚举中的@ElementCollection
【发布时间】:2015-12-01 12:29:06
【问题描述】:

我正在尝试在可嵌入对象中使用 elementCollection,但 JPA2.0 似乎忽略了它。

如果我进行调试,我会看到可嵌入对象中的集合已填充,但不会持久化,并且 JPA 不会引发任何异常。由于某种我找不到的原因,它似乎忽略了它。

如果我将该 elementCollection 放置在基本实体(包含可嵌入对象)中,则保存该集合。

这是为什么?我怎么能改变它?

代码示例:

@Entity
@Table(name =  "PRELEVATION_SURVEY")
public class PrelevationSurvey extends Domain {

    @Embedded
    private GeneralPatientInformation generalPatientInformation;

}

@Embeddable
public class GeneralPatientInformation {

    @ElementCollection(
        fetch = FetchType.EAGER)
    @CollectionTable(name = "CAUSES_OF_DEATH",
        joinColumns = @JoinColumn(name = "SURVEY_ID"))
    @Column(name = "CAUSE_OF_DEATH")
    @Enumerated(EnumType.STRING)
    private List<CauseOfDeath> causesOfDeath;

}

再一次.. 如果我将它们放在 PrelevationSurvey 上,死亡原因仍然存在,但如果我将它们放在 embedabble 中则不会。

【问题讨论】:

    标签: java hibernate jpa enums


    【解决方案1】:

    用以下代码修复它:

    public PrelevationSurvey save(PrelevationSurvey prelevationSurvey) {
        PrelevationSurvey attachedPrelevationSurvey = super.save(prelevationSurvey);
        attachedPrelevationSurvey.getGeneralPatientInformation().setCausesOfDeath(prelevationSurvey.getGeneralPatientInformation().getCausesOfDeath());
        return attachedPrelevationSurvey;
    }
    

    保存方法调用“entityManager.merge(entity)”。 我自己正在合并集合。

    另见:https://hibernate.atlassian.net/browse/HHH-6143

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 2013-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多