Hi Adam, If you call merge on a new entity it will act identically to persist - ie we'll generate an insert statement instead of an update. Unless OpenJPA detects a matching row in the database in which case we'll update that row.
hth -Mike On Mon, Apr 7, 2008 at 6:09 PM, Adam Hardy <[EMAIL PROTECTED]> wrote: > So theoretically, a merge will also persist? > > Actually I started out this project assuming it would, but because it has > to work with TopLink as well OpenJPA, I stopped using merge() when I came > across a bug in the TopLink implementation. > > But TopLink aside, thanks for the reminder. > > > > > Michael Dick on 07/04/08 17:06, wrote: > > > It looks like the persist is being cascaded to the detached entity. If > > that's the case then we're throwing the exception per these bullets in > > the > > JPA spec : > > > > 3.2.1 Persisting an Entity Instance > > A new entity instance becomes both managed and persistent by invoking > > the > > persist method on it or > > by cascading the persist operation. > > The semantics of the persist operation, applied to an entity X are as > > follows: > > <snip> > > • If X is a detached object, the EntityExistsException may be thrown > > when the persist > > operation is invoked, or the EntityExistsException or another > > PersistenceException > > may be thrown at flush or commit time. > > • For all entities Y referenced by a relationship from X, if the > > relationship to Y has been annotated > > with the cascade element value cascade=PERSIST or cascade=ALL, the > > persist > > operation is applied to Y. > > > > If you were to merge the new entity instead of persisting it then the > > merge > > action would be cascaded to the parent entity. It would become managed, > > but > > that might be one way to resolve the issue you're hitting. > > > > -Mike > > > > On Mon, Apr 7, 2008 at 7:09 AM, Adam Hardy <[EMAIL PROTECTED]> > > wrote: > > > > I've got an issue with the persist operation, when I use a detached > > > entity > > > as one of the entity's referenced entities. > > > > > > OpenJPA throws the > > > org.apache.openjpa.persistence.EntityExistsException: Attempt to > > > persist > > > detached object > > > > > > "[EMAIL PROTECTED] > > > ". > > > > > > The situation is this: my MVC layer has received a new entity which it > > > must create. The parent entity for this is found in a cache, in a > > > detached > > > state. > > > > > > What I'd like to know, is why is JPA forcing me to merge this detached > > > entity before allowing me to persist the new child? > > > > > > It means I can't use the cache, or I have to program the DAO to merge > > > all > > > referenced entities. This latter option seems like a job that JPA > > > should be > > > doing. JPA knows this parent is a detached entity, so why can't it > > > merge the > > > managed entity? > > > > > > I can't see any text in the EJB spec that would mandate this > > > behaviour, > > > yet Hibernate does it too. > > > > > >
