Monday, May 12, 2014

Difference between load() and get() methods in Hibernate

Difference between load() and get() methods in Hibernate

Dont use load() when you are not sure about the object exists/record exists in database.
When you call load() method if the object is not exist/unique id is not found in DB, then it will throw an exception.

When you call get() method if the object is not exist then it will return NULL reference.

Hibernate persisting transient object to database - Equality and Identity of persistent object

Hibernate persisting transient object to database How it behaves?.

It is not appropriate to save an object that has already been persisted. Equally, it is not
appropriate to update a transient object. If it is impossible or inconvenient to determine the
state of the object from your application code, you may use the saveOrUpdate() method.

When we have a persistent object in Hibernate,
that object represents both an instance of a class in a particular Java virtual machine (JVM)
and a row (or rows) in a database table (or tables).
Requesting a persistent object again from the same Hibernate session returns the same
Java instance of a class, which means that you can compare the objects using the standard Java
== equality syntax. If, however, you request a persistent object from more than one Hibernate
session, Hibernate will provide distinct instances from each session, and the == operator will
return false if you compare these object instances.

Saturday, May 10, 2014

About Hashmap in Java Collections

Hashmap: Hashmap is collection class where you can store Objects and playing with it.
In Hashmap objects are stored in Key, Value pair.
Keys are also objects, Hashmap stores the keys based on Object hashing.
The better your hashCode implementation the better you can store Hashmap.

Hashmap accepts only one NULL object as key.

NOTE: When you try to add two null keys to hashmap it takes only the last added entry.


import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

 * @author Satya
public class TestJava {

     * @param args
    public static void main(String[] args) {
        Map map = new HashMap();
        map.put(null, "X");
        map.put(null, "Y");
        //Iterating over entrySet
        for(Entry ent: map.entrySet()){
            System.out.println(ent.getKey() + " , " + ent.getValue());



Output of this program:
null , Y