在Java SE中:not-null属性引用一个null或瞬态值

时间:2018-12-22 22:17:22

标签: java hibernate jpa

我有以下两个实体类:

@Entity
@Table(name = "STAT_SETS")
public class StatisticSet extends ClientServerSharedObject {
    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="statSet")
    @MapKey(name = "name")
    @MapKeyEnumerated(EnumType.STRING)
    private Map<AbilityName, Ability> abilities = new EnumMap<>(AbilityName.class);

    public StatisticSet(Race race) {
        for (AbilityName abilityName : AbilityName.values()) {
            abilities.put(abilityName, new Ability(abilityName, getModifier()));
        }
    }
}

@Entity
@Table(name = "ABILITIES")
public class Ability extends ClientServerSharedObject {
    @ManyToOne
    @JoinColumn(name = "STAT_SET_ID", nullable = false)
    private StatisticSet statSet;
}

我正在尝试保留StatisticSet的实例,但出现此错误:

org.hibernate.PropertyValueException: not-null property references a null or transient value : org.package.Ability.statSet

现在,我正在将Hibernate与Java SE中的Apache Derby一起使用,并带有RESOURCE_LOCAL事务。当我在带有JTA的Java EE容器中使用它时,此方法工作正常。将链接添加到Ability中的列表时,Hibernate知道要从StatisticSet创建回链接。

为什么会这样?它与缺少JTA有什么关系吗? persist()发生在EntityTransaction的begin()commit()之间。我可以修复它,还是总是必须手动在Java SE中双向设置链接,如下所示:

abilities.put(abilityName, newAbility);
newAbility.setStatSet(this);

1 个答案:

答案 0 :(得分:0)

在保存Abitlity安全statSet之前,该能力优先。