Note that there are some explanatory texts on larger screens.

plurals
  1. POJava EE6/EJB3/JPA2: How to add an existing (but detached) entity to a new entity in ManyToOne?
    primarykey
    data
    text
    <p>I have problems setting existing but detached entities into a ManyToOne relation, Hibernate throws a <code>PersistentObjectException</code> saying: 'detached entity passed to persist: model.persons.Customer'.</p> <p>My use case is as follows:</p> <p>I open a JSF2 view which lets me create an order through a Named bean and a call to a stateless OrderDAO EJB. On that view i can select a customer from a drop down list. The drop down list is populated using another stateless CustomerDAO EJB which fetches a list of customers from the database. I select the customer and the customer is set to the order. When i save the order using the stateless OrderDAO EJB the above mentioned exception is thrown.</p> <p>My entities looks as follows:</p> <pre><code>@Entity public class Order extends AbstractEntity implements Serializable { private static final long serialVersionUID = -8061887078955032972L; @ManyToOne(cascade={CascadeType.MERGE, CascadeType.REFRESH, CascadeType.PERSIST}, optional = false) private Customer customer = null; ... } @Entity @DiscriminatorValue("customer") public class Customer extends Person implements Serializable { private static final long serialVersionUID = 2788107135840578322L; @OneToMany(cascade=CascadeType.ALL, mappedBy="customer") private List&lt;Order&gt; orders = null; ... } </code></pre> <p>And the code involved looks as follows:</p> <pre><code>@Named @ConversationScoped public class OrderController implements Serializable { private static final long serialVersionUID = -4868506512979135651L; @EJB private OrderEJB orderBean; private Order order; ... public Order getOrder() { if (order == null) { if (id == null) { order = orderBean.create(); } else { order = orderBean.findById(id); } } return order; } public String saveOrder() { order = orderBean.save(order); return "savedOrder"; } } @Stateless public class OrderEJB extends GenericDAO&lt;Order&gt; { } public class GenericDAO&lt;T extends AbstractEntity&gt; { public T create() { try { return getClassType().newInstance(); } catch (Exception e) { logger.log(Level.SEVERE, "Error creating new instance of "+getClassType(), e); } return null; } public T save(T entity) { if (entity.getId() == null) { saveNew(entity); } else { entity = update(entity); } return entity; } private void saveNew(T entity) { em.persist(entity); } private T update(T entity) { return em.merge(entity); } } </code></pre> <p>Is the fact that i use two different EJBs, one for the order and one for the customer a problem? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload