* @param document the document to persist. The contact must be set
* @return the new version of the document after persisted (the contact is
* updated with the lastest version of the contact)
*/
public Document addDocument(Document document) {
Contact contact = document.getContact();
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
// remove the document if exist
if (document.getIdDocument() != null) {
contact.getDocumentLinks().remove(document);
}
// merge the document, add it and merge the contact
document = em.merge(document);
contact.getDocumentLinks().add(document);
contact = em.merge(contact);
em.getTransaction().commit();
em.close();
// store the contact in the document as a return value
document.setContact(contact);