users@glassfish.java.net

Re: Basic JPA Question

From: <glassfish_at_javadesktop.org>
Date: Thu, 18 Oct 2007 08:30:34 PDT

> > For example, let's say Server "A" has a list of
> CPU's containing CPU's with ID's "1" and "2".
> Another Server, "B", also has a list of CPU's which
> also contains CPU's with ID's "1" and "2".
>
> Do you mean that a CPU "1" in Server "A" is not the
> same as in Server "B"?

Correct, Cpu "1" in Server "A" is different than Cpu "1" in Server "B".

> > As far as merging goes, from what I have seen,
> let's say you persist Server "A", above The child
> entities, CPU's with ID's "1" and "2" are also
> persisted to the database. If I want to update
> Server "A", but now it only has 1 single child CPU,
> with ID "3", if I did a simple merge, I would have
> CPU "1", "2", and "3" in the database instead of just
> having CPU "3" like I intended.
> >
>
> You can do em.find(<Server "A">), then compare the
> collections of CPUs and adjust accordingly. It's not
> pretty but better than remove/insert solution.

Yes, that could work (and it certainly isn't pretty ;) but I just assumed that JPA had some mechanism that took care of this. Are you saying that I'm better off doing something like:

Server server = em.find(Server.class, "A");
server.getCpu().clear();
server.getCpu().addAll(newserver.getCpu());
em.merge(server);

?
[Message sent by forum member 'wfsaxton' (wfsaxton)]

http://forums.java.net/jive/thread.jspa?messageID=240962