Suggested Pages

Thursday, June 14, 2012

EntityManager (JPA) vs Session (Hibernate)

In this post i will show how to modify a detached object taking information from Hibernate and JPA reference site.

Session



GET

  • If it finds an object in the session with the same type and the same id in the session of passed object, it returns this persistent instance; otherwise it hits the database to retrieve the object;
  • If it can't find the object in session or in the database, it returns null.
You can use get() when you are not certain that the persistent object exists and when you want to test the return value to see if it’s null.

LOAD

  • If it finds an object in the session with the same type and the same id in the session of passed object, it returns this persistent instance; otherwise it doen't hit the database to retrieve the object, it returns a proxy of the given entity with the given identifier; to access the state of the proxy, you have to use accessor methods of the object that perform a query to the database. If the requested object does not exist an exception is thrown when the object is first accessed.
  • If it can't find the object in the session or in the database, an exception is thrown;
You can use load() when you are certain that the persistent object exists and when you are not interested into the persistence state of the entity. You can use the proxy just as reference to create relationships with other entities.

SAVE

  • If the entity has a generated identifier, this value is associated to the instance when save() is called;
  • If the entity has no identifier, the application-assigned key has to be set before call save() methods;
  • It inserts the entity into the session, but if an entity with the same identifier exists in the session an exception is thrown;
  • When the transaction commits, the entity will be written into the database, but if the primary key already present in the table, it cannot be inserted and an exception is thrown.
See save.

UPDATE

  • If the entity has no identifier (transient object), it will throw an exception;
  • If the entity has an identifier update() makes the entity becomes managed;
  • If there is a persistent instance with the same identifier, an exception is thrown.
See update.

SAVEOrUPDATE

  • It will INSERT or UPDATE depending on the value of the objects identifier.
  • If the entity has no identifier it will call save();
  • If the entity has an identifier it will call update().
  • If the identifier given to the entity is not in the database and an update() is performed, an exception will occurs because you are updating a record that doesn't yet exist.

See saveOrUpdate.

MERGE

  • If a persistence instance exists with the same identifier currently associated with the session, it copies the state of the detached object into the existing persistent instance. The given instance does not become associated with the session;
  • If there is no persistent instance currently associated with the session, try to load it from the database. Then it copies the state of the detached object into the returned persistent instance;
  • If the given instance is unsaved, save a copy of and return it as a newly persistent instance.


EntityManager



PERSIT

  • If the entity has a generated identifier, this value is associated to the instance when persist() is called;
  • If the entity has no identifier, the application-assigned key has to be set before call persist() methods;
  • It inserts the entity into the persistence context, but if an entity with the same identifier exists in the persistence context an exception is thrown;
  • When the transaction commits, the entity will be written into the database, but if the database already contains another entity of the same type with the same primary key, an exception is thrown.


FIND

  • It looks for a entity with the given id (primary key) into the persistence context;
    • If no entity is found in the persistence context, the entity is searched in the database;
  • If no entity is found in the database or in the persistence context, it returns null.
  • if entity is found in the database or in the persistence context, a managed entity returns.


GET_REFERENCE

  • It no entity is found in the persistence context with the given id (primary key), the entity is not searched in the database. A hollow object is initialized with the valid primary key and all its other persistent fields are uninitialized. The object content is retrieved from the database and the persistent fields are initialized, lazily, when the entity object is first accessed. If the requested object does not exist an EntityNotFoundException is thrown when the object is first accessed;
  • If an entity is found in the persistence context, a managed entity returns.


MERGE

  • If the passed object is a transient object it inserts the passed object into the database;
  • If the passed object is a detached object, it loads an entity from the persistence context. If an entity with the same identity (i.e. same type and primary key) is in persistence context, the content of the detached entity object is copied into the existing managed entity;
  • If no entity with the same identity is in the persistence context, a new managed entity object is constructed.
The detached object itself, however, remains unchanged and detached.

No comments :

Post a Comment

Suggested Pages