contains() and isConnected methods in Hibernate

contains(Object object): Used to check whether the instance is associated with the session or not.

Example on contains(object)

Output:

isConnected(): Used to check whether session is currently connected or not.

Example on isConnected()

Output:

 

clear(), evict() and close() methods in Hibernate

clear():  Completely clear the session and is used to dissociate/disconnect all the objects from the session.

Example on clear() Database table before execution

 

Database table after execution

 After calling session.clear(), emp1 and emp2 objects are disconnected from the session object. i.e emp1 and emp2 objects are moved from persistent state to detached […]

saveOrUpdate() method in Hibernate Session

saveOrUpdate

If the record is not present in the database, it will call save() method and inserts the record in the database. If the record is present in the database, it will call update() method and updates the record in the database. Example on saveOrUpdate: Before executing the EmployeeSaveOrUpdateExample.java in dabase

Execute EmployeeSaveOrUpdateExample.java

After […]

When update() method has to call?

Transient state? Transient state means, Object is neither associated with Session nor present in Database. When you don’t have record in the database, then you will not be able to update the record. So when the object is transient state you will not call update() method. Persistent state? Persistent state means, Object is associated with […]

load() and get() methods of Hibernate Session

Hibernate Session provides two methods to access the object (session.get() & session.load()). Both looks like same but there are differences between load () and get () methods. Let’s have a look at those. load(): load() method throws hibernate.ObjectNotFoundException (which is Un-checked exception) if object not found in cache as well as in database. load() method […]

How to generate Random passwords using java.util.Random API?

in this example we will see sample program to  generate Random passwords using java.util.Random API

Output

 

How to pretty print XML from Java?

I have an xml which is non-format as below. How i can make it nicely formatted xml.

Pass the books.xml in the below program , run the program and see output.

Output: Now we can see that books.xml is printed in nice format.

 

Hibernate Persistence/Entity object life cycle

Persistence Object has three life cycle states 1. Transient state 2. Persistent state 3. Detached state Transient state: Object is neither associated with Session nor present in Database. Persistent state: Object is associated with Session and also present in Database. Detached state: Object is not associated with Session, but present in Database. Transient state Example: EMPLOYEE table Application […]

Hibernate: Many To Many Example using Annotation

In this tutorial, you will learn how to work with Many to Many table relationship in Hibernate using Annotation. Tools & technologies used in this article: Java 1.6 Maven 3.2. Hibernate 4.x Oracle 11.2.0 Eclipse Many-to-Many relationship between TABLE A and TABLE B is as follows. Each row in a TABLE A is related to 0,1 or […]

Hibernate: Many To Many Example using XML mapping

In this tutorial, you will learn how to work with Many to Many table relationship in Hibernate using XML mapping. Tools & technologies used in this article: Java 1.6 Maven 3.2. Hibernate 4.x Oracle 11.2.0 Eclipse Many-to-Many relationship between TABLE A and TABLE B is as follows. Each row in a TABLE A is related to 0,1 […]