Hibernate映射到同一个实体并不允许空值

时间:2018-06-19 08:45:43

标签: java hibernate

我正在从Hibernate 3.6.5升级到5.1。

我有一个类Lead的映射,如下所示:

@Entity
@Table(name="lead", uniqueConstraints = { })
public class Lead implements Serializable {

    private int leadid;

    // More fields

    private Set<Lead> duplicateLeadChildren;
    private Lead duplicateLeadParent;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="leadid", unique=true, nullable=false, insertable=true, updatable=true)
    public int getLeadid() {
        return this.leadid;
    }
    public void setLeadid(int leadid) {
        this.leadid = leadid;
    }

    // More setters and getters

    /**
     * Returns a set of leads that has been marked as duplicates of this lead.
     * @return
     */
    @OneToMany(fetch=FetchType.LAZY)
    @JoinTable(name="leadduplicate"
        , joinColumns={@JoinColumn(name="parentleadid")}
        , inverseJoinColumns={@JoinColumn(name="childleadid")})
    @NotFound(action=NotFoundAction.IGNORE)
    public Set<Lead> getDuplicateLeadChildren(){
        return duplicateLeadChildren;
    }

    public void setDuplicateLeadChildren(Set<Lead> duplicateLeadChildren) {
        this.duplicateLeadChildren = duplicateLeadChildren;
    }

    /**
     * Returns a lead if this lead has been marked as a duplicate.
     *
     * The returned lead is the lead that should be used as the main lead in all future contact with the company.
     * @return
     */
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinTable(name="leadduplicate"
        , joinColumns={@JoinColumn(name="childleadid")}
        , inverseJoinColumns={@JoinColumn(name="parentleadid")})
    @NotFound(action=NotFoundAction.IGNORE)
    public Lead getDuplicateLeadParent(){
        return duplicateLeadParent;
    }

    public void setDuplicateLeadParent(Lead duplicateLeadParent) {
        this.duplicateLeadParent = duplicateLeadParent;
    }
}

这已经有效多年了。升级到5.1.14.Final或5.2.17.Final后,如果我创建了一个Lead并尝试保存它,

final Lead lead = new Lead();
// Removed setting some other fields removed from the class above
session.saveOrUpdate(lead);

我收到此错误:

Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value : ***.Lead.duplicateLeadParent

映射并未声明它必须为非null。将默认情况下optional=true明确设置为duplicateLeadParent的@ManyToOne注释并不会有帮助。

这听起来有点类似于这里的问题:https://hibernate.atlassian.net/browse/HHH-11596但是使用比该问题的修复版本晚的版本(5.2.10)并没有帮助。

0 个答案:

没有答案