July 13 2015

Hibernate cascade delete

Hibernate cascade delete

When cascade=”delete” is declared on association, hibernate deletes all the persistent instances of the association (children) whenever the delete operation is performed on object. If you don’t not use cascade delete, you have to delete all the child objects one by one explicitly before deleting the parent object.

Hibernate cascade delete example

In the below example, ’CascadeType.DELETE’ is declared for the association. Whenever delete operation is performed on Student object, all the associated StudentMarks records will be deleted along with Student record.

select * from student;

STUDENT_ID STUDENT_NAME FATHER_NAME DATE_OF_BIRTH COURSE
105 PAUL GEORGE 5/22/2015 MS

select * from student_marks; Continue reading

July 13 2015

Hibernate cascade delete orphan

Hibernate cascade delete orphan

Deletes the persistent instance that has been removed (from collection) or dereferenced (object).The orphan delete allows us to delete the child entity by removing or dereferencing it from parent instead of deleting the child entity’s directly.

Hibernate cascade delete orphan Example

select * from student;

STUDENT_ID STUDENT_NAME FATHER_NAME DATE_OF_BIRTH COURSE
105 PAUL GEORGE 5/22/2015 MS

select * from student_marks; Continue reading

May 27 2015

Hibernate cascade types

Hibernate cascade types

Cascading means that if you do any operation on an object it acts on the related objects as well. If you insert, update or delete an object the related objects (mapped objects) are inserted, updated or deleted.

Hibernate does not navigate to object associations when you are performing an operation on the object. If you do not use cascade, you have to do the operations like save, update or delete on each and every object explicitly.

To enable cascading for related association, the cascade attribute has to be configured for association in mapping metadata

Hibernate cascade types Continue reading

May 8 2015

org.hibernate.hibernateexception: /hibernate.cfg.xml not found

org.hibernate.hibernateexception /hibernate.cfg.xml not found

This exception will be thrown when hibernate is not able to locate hibernate.cfg.xml file in the root directory of your application’s classpath.

Example 

In the below example, if the hibernate.cfg.xml is not available in your src folder org.hibernate.hibernateexception will be thrown

HibernateUtility.java Continue reading

May 8 2015

Difference between get and load in hibernate

Difference between get and load in hibernate

Hibernate Session provides get and load methods to retrieve the persistent objects from database by identifier.

Object load(Class theClass,Serializable id)  throws HibernateException

Object get(Class clazz,Serializable id) throws HibernateException

Following are the differences between load and get method in hibernate

  1. If load method cannot find the object in the cache or database, an ObjectNotFoundException is thrown whereas get method returns null if it is not able to find the object.

Continue reading

April 30 2015

Hibernate object states – Transient,Persistent and Detached

Hibernate object states

The following are the hibernate object states

Transient object state

An object which is not associated with hibernate session and does not represent a row in the database is considered as transient. It will be garbage collected if no other object refers to it.

You have to use save, persist and saveOrUpdate methods to persist the transient object.

Persistent object state

An object which represents a row in the database or a transient object which is associated with session is considered as persistent object. It is the existing object which is retrieved from database or a transient object associated with session and which is about to be saved. Continue reading

April 30 2015

Difference between save and persist in hibernate

 Difference between save and persist in hibernate       

The save and persist methods are used to persist the transient instance. Save and persist method results a sql insert query. If any properties are updated after the save or persist method and before the transaction is committed or session is flushed, the changes will be saved to the database using update query.

public Serializable save(Object object) throws HibernateException

public void persist(Object object) throws HibernateException

Difference between save and persist methods Continue reading

April 28 2015

Named query in hibernate

Named query in hibernate

A named query is query which can be defined once and use multiple times. Named queries are useful if you want to reuse complex or same query at various places. Named queries stored in hibernate mapping files are easier to maintain than queries scattered all over the Java code. It has a name to be identified and the name have to be unique.

Hibernate named queries are easy to cache if necessary and provides a slight performance advantage. The getNamedQuery() method obtains a Query instance for a named query.

Example Continue reading

April 27 2015

Hibernate createsqlquery example

Hibernate createsqlquery example

Hibernate allows us to write the queries in native sql. You can execute sql queries from the session with the createSQLQuery() method on the Session interface. The createSQLQuery is used to create a SQL query using the database specific features.

public SQLQuery createSQLQuery(String sqlString) throws HibernateException

Scalar queries

SQL query is to get a list of scalars (values).By default SQL result set rows are returned as Object array.

Example Continue reading