not-null属性引用一个null或瞬态值


81

在使用休眠保存父/子对象时遇到麻烦。任何想法将不胜感激。

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>

InvoiceManager.java

class InvoiceManager {

    public Long save(Invoice theInvoice) throws RemoteException {
        Session session = HbmUtils.getSessionFactory().getCurrentSession();
        Transaction tx = null;
        Long id = null;
        try {
            tx = session.beginTransaction();
            session.persist(theInvoice);
            tx.commit();
            id = theInvoice.getId();
        } catch (RuntimeException e) {
            if (tx != null)
                tx.rollback();
            e.printStackTrace();
            throw new RemoteException("Invoice could not be saved");
        } finally {
            if (session.isOpen())
                session.close();
        }
        return id;
    }
}

发票.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;
    }
}

InvoiceItem.java

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;
    }
}

Answers:


47

每天InvoiceItem必须有一个Invoice连接到它因为not-null="true"许多一对一的映射。

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


80

对于追随者而言,此错误消息也可能意味着“您引用了尚未保存到DB的异物”(即使该异物已经存在并且为非null)。


1
我该如何解决呢?示例:我有一个带有Entity_B列的Entity_A,在Entity_B中有一个Entity_A列,该关系是从A到B的@OneToMany,当我尝试更新Entity_A时却给了我这个异常,我该如何更新Entity_A?
FAndrew '16

@FAndrew首先保存Entity_B吗?
rogerdpack '16

3
例如,如果我有100个Entity_B,则搜索所有已编辑的Entity_B,如果已编辑,则更新它?如果我只是更新Entity_A会更好吗?
FAndrew '16

6

这可能很简单:

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

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


2

检查hbm文件中主键/对象ID的未保存值。如果您通过hibernate框架自动创建了ID,并且在某个地方设置了ID,则会抛出此错误。默认情况下,未保存的值为0,因此,如果将ID设置为0,则会看到此错误。


0

我遇到了同样的错误,但终于解决了,实际上,我没有设置已经保存到另一个实体的对象实体,因此它获取的外键的对象值为空。



-10

将该变量设为瞬时。您的问题将得到解决。

@Column(name="emp_name", nullable=false, length=30)
    private transient String empName;

3
您是在说要解决错误而不是解决问题。
Karthik R
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.