9.1 Overview
In this chapter we will discuss about Hibernate operations like save, delete, update, list etc on persistent object. We will discuss about the operations that can be used to change the state of an object along with the key differences between certain operations.
9.2 Object States and Life Cycle
We have discussed earlier also that persistent objects can be in one of the three states.
- Transient – any object which has just instantiated using new keyword and has not associated with any session.
- Persistent - Persistent instances are the objects that are currently mapped with a session and has a corresponding row in a relational database. Any object can be in persistent state if object has been loaded by database or has been saved.
- Detached- Detached instances are the objects that are mapped to the session or persistent context but session is closed later. In other word , objects whose mapped session is closed.
9.3 Working with persistent Objects.
There are several operations that can be performed on an object to persist the object.
Refer Chapter 3 and 5 to understand how to create a java application project
9.3.1 Save an Object to database.
We can call save() method to save an object in to the database and also this operation makes a transient object to persistent object.
- save method returns identifier immediately because primary object is saved when save method is called.
- if there are other objects mapped from the primary object, they are saved when we commit the transaction or flush the session.
Lets take an example of Book Object.
a. Define a Book.java class
package com.tutorial.hibernate; public class Book { private int isbn; private String name; private String author; public int getIsbn() { return isbn; } public void setIsbn(int isbn) { this.isbn = isbn; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } }
b. Define a book.hbm.xml class
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.tutorial.hibernate.Book" table="BOOK"> <id name="isbn" type="int" column="isbn"> <generator class="native"/> </id> <property name="name" column="name" type="string"/> <property name="author" column="author" type="string" /> </class> </hibernate-mapping>
c. Create SaveBook.java –
We should avoid saving object outside the transaction boundaries but even if we do like below code then we need to make sure below two points else data will not be saved.
- Set the hibernate.connection.autocommit = true in hibernate.cfg.xml
- Calling session.flush();
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import com.tutorial.hibernate.Book; public class SaveBook { public static void main(String a[]) { Configuration cfg = new Configuration().configure(); SessionFactory factory = cfg.buildSessionFactory(); Session session = factory.openSession(); Book book = new Book(); book.setName("Hibernate Tutorial!!"); book.setAuthor("Anonymous"); session.save(book); session.flush(); factory.close(); } }
d. Run SaveBook.java
e. Update SaveBook.java to run within transaction boundaries.
We do not need hibernate.connection.autocommit = true anymore
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import com.tutorial.hibernate.Book; public class SaveBook { public static void main(String a[]) { Configuration cfg = new Configuration().configure(); SessionFactory factory = cfg.buildSessionFactory(); Session session = factory.openSession(); Transaction tx = session.beginTransaction(); Book book = new Book(); book.setName("Hibernate Tutorial!!"); book.setAuthor("Anonymous"); session.save(book); tx.commit(); factory.close(); } }
NOTE: Insert Statement is not getting executed immediately once save() method is called instead hibernate does keep a track of it and executes the statements when necessary. It is recommended (not required )to fully initialize the object before calling save() object. Because changing state of an object after calling save() will require additional update() statement.
For example below code will fire insert statement and then update statement.
book.setName(“”);
session.save(book);
book.setAuthor(“”);