DAO insertOrUpdate() method: how to implement using JPA?
I have a SLSB that explicitly saves a data transfer object to disk, if it already exists, or creates a new one on disk if it doesn't.
The DTO is now an @Entity, and I'd like to implement this DAO to use the JPA.
I am getting quite confused with how to do this. Simply calling:
...seems to do the right thing in all cases. Could this really be this simple? When, if ever, would I be required to invoke the persist() method? I'm feeling queasy that I never invoke this in the code anywhere.
I should mention that the object being inserted/updated is always coming from a fat client, and is hence pretty much guaranteed to be detached. What puzzles me is the behavior of the merge() method in the case of this-is-a-brand-new-never-persisted-before DTO. I am surprised that this works. Should it? Is it guaranteed to? Is this Glassfish-specific behavior?