I'm at the point where I have a webservice up and running that allows me to save a single object to the database, via an EntityManager. After that, I'm having some problems.
1) em.contains() does not seem to work. I have a web service "insertOrUpdate" which does the following:
Code:
if(em.contains(server)) {
utx.begin();
em.merge(server);
utx.commit();
} else {
utx.begin();
em.persist(server);
utx.commit();
}
When I run it the first time, em.contains() returns false and it does a persist. When I run it the second time, using the same object, em.contains() still returns false and the persist fails because the entity is already in the DB.
2) em.merge() does not delete items in a collection (inside the entity) that are no longer there, it simply adds any additional ones it finds. Is there an easy way to do this or do I have to remove an entity and persist it again?
3) Why does em.remove() require an entity as an argument. Isn't it enough to just have the entity's primary key and be done with it? Or do I have to do something like get the entity via em.find() then use the entity it returns and do a em.remove() on that entity.