[hibernate] null이 아닌 속성은 null 또는 임시 값을 참조합니다.

최대 절전 모드로 부모 / 자식 개체를 저장하는 데 문제가 있습니다. 어떤 아이디어라도 대단히 감사하겠습니다.

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

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

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



답변

모든는 InvoiceItem있어야 Invoice때문에의 첨부 not-null="true"대일 매핑.

따라서 기본 아이디어는 코드에서 명시적인 관계를 설정해야한다는 것입니다. 이를 수행하는 방법에는 여러 가지가 있습니다. 수업에서 나는 setItems방법을 봅니다. addInvoiceItem방법이 보이지 않습니다 . 항목을 설정할 때 집합을 반복 item.setInvoice(this)하고 모든 항목을 호출 해야합니다. addItem메서드 를 구현하는 경우 에도 동일한 작업을 수행해야합니다. 또는 InvoiceItem컬렉션 의 모든 송장을 설정해야합니다 .


답변

팔로어의 경우이 오류 메시지는 “아직 DB에 저장되지 않은 외부 개체를 참조하고 있음”을 의미 할 수도 있습니다 (해당 항목이 있고 null이 아니더라도).


답변

이것은 다음과 같이 간단 할 수 있습니다.

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

그러나 지속되는 동안 “Some_Column”이 기본 또는 외래 키가 아닐지라도 “Some_Column”의 값은 null입니다.


답변

hbm 파일에서 기본 키 / 개체 ID의 저장되지 않은 값을 확인하십시오. Hibernate 프레임 워크에 의해 자동화 된 ID 생성이 있고 ID를 어딘가에 설정하면이 오류가 발생합니다. 기본적으로 저장되지 않은 값은 0이므로 ID를 0으로 설정하면이 오류가 표시됩니다.


답변

나는 같은 오류가 발생했지만 마침내 해결했습니다. 실제로 다른 엔티티에 이미 저장된 Object Entity를 설정하지 않았으므로 외국 키에 대해 얻은 Object 값이 null입니다.


답변

@Basic (optional = false) 속성을 제거하거나 부울 @Basic (optional = true)를 업데이트하여 해결했습니다.


답변

그 변수를 일시적으로 만드십시오. 문제가 해결 될 것입니다 ..

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