Thank you for replies.
It's odd that I can't find anything on em.merge indicating that it will persist if the object doesn't exist, or update if it does.
One thing I didn't go into detail about, but I will now to see if it changes the use of em.merge in all cases..
I use a delegation model behind my entity beans. That is, I have a POJO model object that is passed around from my rest front end to the
EJB session bean back end. Because I package up my REST API as an SDK for client side developers as well (makes it easy with Jersey to use to work with my API), I use the pojo model without any ejb annotations in it as DTOs basically. The benefit is that I can get a REST request which builds out the POJO in a representation class, then pass the pojo as is to the entity as the model. Using method annotations on the entity beans, I delegate the get/set calls to the model get/set methods. So it looks something like:
Basically, the above allows me to reuse the User POJO object not only as a backing model in the rest representation class and the user entity bean, but it can also be used in other areas.. for example a
struts 2 form being submitted with the user data fields, can use the same pojo to auto populate with. More so, I can bundle up the User pojo in a rest client side SDK allowing developers to utilize the same pojo when say creating a User. I could be wrong, but if I were to use the UserEntity object all over, the SDK that wraps it up would require the dependency on the javax.ejb.* entity annotations in order to work on the client side. I don't want the ejb dependency to be required on the client side.
So..the reason to explain all this? Well, in my case, this way of doing things means EVERY entity is "detached" via the pojo model that is used in between the tiers. So if I return a List<User> response back which would be something like <users><user><username>...</username></user><user><username>...</username></user></users> and the client side decided to add a couple new users, but also update some existing users.. they would send in a new List<User> with update users and new users..perhaps some removed. THUS.. the whole point of this post is to figure out if there is a way to update existing entities, add new ones and perhaps even delete ones not in the list passed in.. and if em.merge() does this? Or do I have to loop through the list, looking up each object to see if it exists.. if so, update it (merge) and if not, persist it. As well.. to handle deletion I'd have to get the list of ALL objects in the DB and figure out those missing in the passed in list to remove them from the DB.
Sorry for the long winded explanation, not sure how else to detail what it is I am trying to do.
Thank you.