Hibernate Working With Objects

Hibernate is one of the obect oriented mappping solution which allows us to manage the state of an object.

State Of Object:

transient: A new instance of a persistent class which is not associated with a Session and has no representation in the database and no identifier value is considered transient by Hibernate.

persistent:You can make a transient instance persistent by associating it with a Session. A persistent instance has a representation in the database, an identifier value and is associated with a Session.

detached: Once we close the Hibernate Session, the persistent instance will become a detached instance.

How to make an object persistent?

We can make an object persistent using below two methods:

1) persist(): It makes a transient instance persistent, but doesn't guarantee that the identifier value will be assigned to the persistent instance immediately, the assignment might happen at flush time. It also guarantees that it will not execute an INSERT statement if it is called outside of transaction boundaries.

Employee empObj = new Employee();
empObj.setName("Ankit");
empObj.setSex("M");
sess.persist(empObj);

2) save() does guarantee to return an identifier. With this execution happens immediately.

Employee empObj = new Employee();
empObj.setName("Ankit");
empObj.setSex("M");
Long generatedId = (Long) sess.save(empObj);

Alternatively, you can assign the identifier using an overloaded version of save().

How to load an Object?

We can use load() and get() method to load and object.

1) load() method is used to load an object. It always return proxy without hitting database. If no row found then it will throws ObjectNotFoundException.

e.g

long id = 121;
Employee emp = (Employee) sess.load( Employee.class, new Long(id) );

emp.getX(); //will throw exception ObjectNotFoundException if no row found in DB.

//or

Employe emp = new Employee();
sess.load( emp, new Long(id) );

emp.getX(); //will throw exception ObjectNotFoundException if no row found in DB.

2) get() method always hit DB and return the real object.

e.g

Employee empObj = (Employee)session.get(Employee.class, new Integer(2));

empObj.getXX(); //will throw NullPointer exception if no row exist in DB.

Modifying persistent objects:

We can modify the presisted object and save the update in database.

e.g

Employee emp = (Employee) sess.load( Employee.class, new Long(69) ); 
emp.setName("Ankit");
sess.flush();  // changes to emp are automatically detected and persisted

Deleting persistent objects

We can delete persistent object as

sess.delete(emp);

Replicating object between two different datastores

We may need to deal with different databases in one application and may be required the data to be replicated from one to another.

//fetching data from ist database
Session session1 = factory1.openSession();
Transaction tx1 = session1.beginTransaction();
Employee emp = session1.get(Employee.class, catId);
tx1.commit();
session1.close();

//reconcile with a second database
Session session2 = factory2.openSession();
Transaction tx2 = session2.beginTransaction();
session2.replicate(emp, ReplicationMode.LATEST_VERSION);
tx2.commit();
session2.close();

The ReplicationMode determines how replicate() will deal with conflicts with existing rows in the database:

ReplicationMode.IGNORE: ignores the object when there is an existing database row with the same identifier.

ReplicationMode.OVERWRITE: overwrites any existing database row with the same identifier.

ReplicationMode.EXCEPTION: throws an exception if there is an existing database row with the same identifier.

ReplicationMode.LATEST_VERSION: overwrites the row if its version number is earlier than the version number of the object, or ignore the object otherwise.