Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > it.comp.java > #8892

Re: JPA detached entity

X-Received by 10.66.102.8 with SMTP id fk8mr12800261pab.44.1459018528611; Sat, 26 Mar 2016 11:55:28 -0700 (PDT)
X-Received by 10.182.125.37 with SMTP id mn5mr165547obb.10.1459018528566; Sat, 26 Mar 2016 11:55:28 -0700 (PDT)
Path csiph.com!weretis.net!feeder6.news.weretis.net!news.glorb.com!av4no2178824igc.0!news-out.google.com!pn7ni16908igb.0!nntp.google.com!nt3no4819973igb.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups it.comp.java
Date Sat, 26 Mar 2016 11:55:28 -0700 (PDT)
In-Reply-To <nd688e$h2n$1@adenine.netfront.net>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=92.23.78.62; posting-account=MVf4PgoAAACplM_7JiJVCzb6tVRbVBwd
NNTP-Posting-Host 92.23.78.62
References <nd1i1r$25te$1@adenine.netfront.net> <0d59bcc5-4860-4218-b339-7f2f17cea807@googlegroups.com> <nd688e$h2n$1@adenine.netfront.net>
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <b82779cc-87ea-4e38-b180-d05c337b5afc@googlegroups.com> (permalink)
Subject Re: JPA detached entity
From Cicap <forzatonino@gmail.com>
Injection-Date Sat, 26 Mar 2016 18:55:28 +0000
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Xref csiph.com it.comp.java:8892

Show key headers only | View raw


Il giorno sabato 26 marzo 2016 15:01:36 UTC, Davide ha scritto:
> Il 26/03/2016 15:11, Cicap ha scritto:
> > Il giorno giovedì 24 marzo 2016 20:18:05 UTC, Davide ha scritto:
> >> Salve,
> >> sto sviluppando un'applicazione java ee che usa jpa, se nel DAO faccio
> >> una cancellazione (metodo remove()) di un'entità detached viene
> >> giustamente generata un'eccezione che indica che l'entità è detached
> >> mentre invece se faccio un'aggiornamento (metodo merge()) di un'entità
> >> detached non viene generata nessuna eccezione e l'aggiornamento va a
> >> buon fine, come mai succede questo?
> > 
> > Domanda: cosa pensi che faccia l'operazione "merge" in generale? E da quale fonte ti sei formato quell'idea.
> > 
> 
> Scusami, per errore ho risposto sulla tua mail.
> 
> L'operazione merge credo che faccia un update, la fonte è:
> https://docs.oracle.com/javaee/6/api/javax/persistence/EntityManager.html#merge%28T%29

"Merge the state of the given entity into the current persistence context."

Quindi se un'entity e' detached il metodo merge *ritorna* un'entity managed, quindi sincronizzera' il suo stato con il database. E' il motivo per cui esiste "merge": se hai un'entity detached ci devi fare il "merge" nel persistence context prima di poterci operare (solo se vuoi sincronizzarla verso il db ovviamente).

Attenzione che merge ritorna un'entity managed, mentre l'entity passata come parametro rimane detached (se era detached).

Dovresti prima studiarti come minimo il funzionamento di JPA riguardo al lifecycle delle entity, perche' e' evidente che ti mancano i principi basilari.

Esempio:

http://www.objectdb.com/java/jpa/persistence/managed 

Back to it.comp.java | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

JPA detached entity Davide <a@a.com> - 2016-03-24 21:18 +0100
  Re: JPA detached entity Cicap <forzatonino@gmail.com> - 2016-03-26 07:11 -0700
    Re: JPA detached entity Davide <a@a.com> - 2016-03-26 16:02 +0100
      Re: JPA detached entity Cicap <forzatonino@gmail.com> - 2016-03-26 11:55 -0700
        Re: JPA detached entity Davide <a@a.com> - 2016-03-26 20:15 +0100

csiph-web