Hibernate not null属性引用null或瞬态值

Hibernate not null属性引用null或瞬态值,hibernate,Hibernate,使用hibernate保存父/子对象时遇到问题。任何想法都将不胜感激 org.hibernate.PropertyValueException: not-null property references a null or transient value: example.forms.InvoiceItem.invoice at org.hibernate.engine.Nullability.checkNullability(Nullability.java:100)

使用hibernate保存父/子对象时遇到问题。任何想法都将不胜感激

org.hibernate.PropertyValueException: not-null property references a null or transient value: example.forms.InvoiceItem.invoice
    at org.hibernate.engine.Nullability.checkNullability(Nullability.java:100)
        .... (truncated)
休眠映射:

<hibernate-mapping package="example.forms">
    <class name="Invoice" table="Invoices">
        <id name="id" type="long">
            <generator class="native" />
        </id>
        <property name="invDate" type="timestamp" />
        <property name="customerId" type="int" />
        <set cascade="all" inverse="true" lazy="true" name="items" order-by="id">
            <key column="invoiceId" />
            <one-to-many class="InvoiceItem" />
        </set>
    </class>
    <class name="InvoiceItem" table="InvoiceItems">
        <id column="id" name="itemId" type="long">
            <generator class="native" />
        </id>
        <property name="productId" type="long" />
        <property name="packname" type="string" />
        <property name="quantity" type="int" />
        <property name="price" type="double" />
        <many-to-one class="example.forms.Invoice" column="invoiceId" name="invoice" not-null="true" />
    </class>
</hibernate-mapping>
Invoice.java

public class Invoice implements java.io.Serializable {
    private Long id;
    private Date invDate;
    private int customerId;
    private Set<InvoiceItem> items;

    public Long getId() {
        return id;
    }
    public Date getInvDate() {
        return invDate;
    }
    public int getCustomerId() {
        return customerId;
    }
    public Set<InvoiceItem> getItems() {
        return items;
    }
    void setId(Long id) {
        this.id = id;
    }
    void setInvDate(Date invDate) {
        this.invDate = invDate;
    }
    void setCustomerId(int customerId) {
        this.customerId = customerId;
    }
    void setItems(Set<InvoiceItem> items) {
        this.items = items;
    }
}

由于多对一映射中的
notnull=“true”
,每个
Invoice项目
都必须附加一个
Invoice


因此,基本思想是需要在代码中建立明确的关系。有很多方法可以做到这一点。在你的课堂上,我看到了一个
setItems
方法。我没有看到
addInvoiceItem
方法。设置项目时,需要在集合中循环,并对所有项目调用
item.setInvoice(this)
。如果实现
addItem
方法,则需要执行相同的操作。或者您需要以其他方式设置集合中每个
InvoiceItem
的发票。

对于关注者,此错误消息也可能意味着“您的发票引用了尚未保存到DB的外来对象”(即使它在那里,并且不为空).

检查hbm文件中主键/对象ID的未保存值。如果您已通过hibernate framework自动创建ID,并且您正在某处设置ID,则会引发此错误。默认情况下,未保存的值为0,因此如果您将ID设置为0,则会看到此错误。

我收到了相同的错误,但最终解决了它,实际上,我没有设置已保存到另一个实体的对象实体,因此它为foreign key获取的对象值为null。

这可以简单到:

@Column(name = "Some_Column", nullable = false)

但在持久化时,“Some_Column”的值为null,即使“Some_Column”可能不是任何主键或外键。

我通过删除@Basic(optional=false)属性来解决此问题 或 只需更新布尔@Basic(可选=true)


我想这会有帮助。

我该如何解决这个问题?示例:我有一个实体A和一个实体B列,在实体B中我有一个实体A列,关系是从A到B的@OneToMany,当我尝试更新实体A时,会出现此异常,我如何更新实体A?@FAndrew先保存实体B?如果我有100个实体B,我搜索所有已编辑的实体,如果它已编辑,我是否更新它?如果我只更新实体A会更好吗?
public class InvoiceItem implements java.io.Serializable {
    private Long itemId;
    private long productId;
    private String packname;
    private int quantity;
    private double price;
    private Invoice invoice;

    public Long getItemId() {
        return itemId;
    }
    public long getProductId() {
        return productId;
    }
    public String getPackname() {
        return packname;
    }
    public int getQuantity() {
        return quantity;
    }
    public double getPrice() {
        return price;
    }
    public Invoice getInvoice() {
        return invoice;
    }
    void setItemId(Long itemId) {
        this.itemId = itemId;
    }
    void setProductId(long productId) {
        this.productId = productId;
    }
    void setPackname(String packname) {
        this.packname = packname;
    }
    void setQuantity(int quantity) {
        this.quantity = quantity;
    }
    void setPrice(double price) {
        this.price = price;
    }
    void setInvoice(Invoice invoice) {
        this.invoice = invoice;
    }
}
@Column(name = "Some_Column", nullable = false)
cascade = CascadeType.PERSIST