Problem with remove from collection soon after add


can anyone help me to understand why the following does not work?

            Document doc = DocumentDAO.createDocument();
            DocumentLine line2 = new DocumentLineImpl();
            line2.setProduct("my product name");
            line2.setPrice(new BigDecimal(13));
            doc.documentLine.remove(line2); //<-- soon after add, I use smart association

I expected to get a document with zero lines, but I have the following exception:

org.orm.PersistentException: org.hibernate.PropertyValueException: not-null property references a null or transient value: model.db.documents.documentlines.impl.DocumentLineImpl.document

Why? The DocumentLine has been removed from the collection, so why is hibernate trying to save it anyway?

Thank you!

May I know have you also calling save on DocumentLine? If so then it is correct to have that exception since you have removed the reference between Document and DocumentLine. This causing a null FK on DocumentLine which trigger the exception on save. If you won’t save the DocumentLine if removed it from Document.

Best regards,
Rain Wong