3 Replies Latest reply on Feb 15, 2007 12:11 AM by mazz

    Not understanding detached entities and merge()

    None None Master

      Here's the situation:

      I have an entity that refers to other entities, like this:

      @Entity public class Invoice implements Serializable {
       @ManyToOne private transient Customer customer;

      It must implement Serializable so I can send it to message queues.

      I labeled the customer field as transient, because I don't want it to serialize and send the entire Customer object every time (that object has a bunch of its own fields).

      So, because that's transient, this Invoice entity shows up, is de-seriaiized, and the customer field is null of course.

      I then do

      invoice = entityManager.merge(invoice);

      to re-attach this now-detached entity. But when I do that, the customer field gets set to null in the DB. It is taking the state from the un-serialized object and writing over what's in the DB.

      That's not what I want it to do, of course.

      What's the right way to do this kind of thing? An easy work around I have is to use entityManager.find(Invoice.class, invoice.getId()) but that seems wrong.