Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Entity isn't persisted to database

Bernard wrote
> 
> Hi,
> 
> If I understand you correctly then em.persist is wrong for an entity
> that has been merged back into the managed state. persist is used only
> for new entities that have not yet been saved in the database.
> 

Thank you Bernard, I looked that up from
http://en.wikibooks.org/wiki/Java_Persistence/Persisting. Most topics are
written by James Sutherland.



> The EntityManager.persist() operation is used to insert a *new object*
> into the database. persist does not directly insert the object into the
> database, it just registers it as new in the persistence context
> (transaction). When the transaction is committed, or if the persistence
> context is flushed, then the object will be inserted into the database.
> ...
> persist should normally only be called *on new objects*. It is allowed to
> be called on existing objects if they are part of the persistence context,
> this is only for the purpose of cascading persist to any possible related
> new objects. If persist is called on an existing object that is not part
> of the persistence context, then an exception may be thrown, or it may be
> attempted to be inserted and a database constraint error may occur, or if
> no constraints are defined, it may be possible to have duplicate data
> inserted.
> 

Merge Use Case


> Normally merge is not required, although it is frequently misused. *To
> update an object you simply need to read it, then change its state through
> its set methods, then commit the transaction*. The EntityManager will
> figure out everything that has been changed and update the database. merge
> is only required when you have a detached copy of a persistence object. ..
> The merge operation will look-up/find the managed object for the detached
> object, and copy each of the detached objects attributes that changed into
> the managed object, as well as cascading any related objects marked as
> cascade merge.
> 

I will correct this and report if above still occur or not.



--
View this message in context: http://eclipse.1072660.n5.nabble.com/Entity-isn-t-persisted-to-database-tp154017p154019.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.


Back to the top