users@glassfish.java.net

Re: JPA - why persist and merge, why not a sigle save

From: Witold Szczerba <pljosh.mail_at_gmail.com>
Date: Sun, 22 Jul 2007 22:29:50 +0200

I was wondering about this as well... Maybe this is because when you
really want to create new entity and you want to be 100% sure it is
new, you use #persist and if that object actually exists, the #persist
will throw an exception. With #merge, you would have to query database
first to make sure.

What's important to notice, you can always use #merge, even if your
entity is brand new. Also remember, that these two methods act a
little differently: #merge does not change the entity when it is not
managed, it creates another one and returns it, while #persist
actually modifies the object you pass to it. This is important when
you first create new object, then you can pass it another entities to
setup relation and finally you can #persist. The object you did pass
to other entities will change as well. If you would use #persist, then
it would not work.

2007/7/22, glassfish_at_javadesktop.org <glassfish_at_javadesktop.org>:
> I don't understand why we do need two different methods for persisting and merging! Shouldn't persistence context be smart enough to distinguish between New, Manged and Detached entities? In that case all we need would be a Save method to persist/update a New/Managed/Detached entity.
>
> Does anybody know what the rational is behind having two separate methods vs. one?
> [Message sent by forum member 'mohammadwrk' (mohammadwrk)]
>
> http://forums.java.net/jive/thread.jspa?messageID=227617
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>