views:

64

answers:

1

I learnt Hibernate and used it to reduce my Java code to a vast extent and also able to reduce the time spent for DB's. Now what type of query should i use to finish my operations for getting a DB list to be displayed, to update and delete.

My code for deletion is

String newToken = "DELETEUSER";
if(!TokenManager.checkRoleToken(newToken)){
    return;
}
Session session = Main.getSession(); //calling the main method to get sesion 
Leavetable table = new Leavetable; // intialisation of object table
try{
    Transaction tr = session.beginTransaction();
    table = session.createQuery();
    session.delete(table); // deletion of the object and its properties  from selected leaveID
    tr.commit();            
}
finally{
    session.close();
}

My code for Db updation

public void updateLeaveTable( Leavetable leave ) {
    String newToken = "ADDUSER";
    if( !TokenManager.checkRoleToken( newToken ) ) {
        return;
    }
    Session session = Main.getSession(); // calling the main method to get
                                         // session
    try {
        session = Main.getSession();
        Transaction tr = session.beginTransaction();
        session.saveOrUpdate( leave ); // here without query the table gets
                                       // updated How?
        tr.commit();
    }
    finally {
        session.close();
    }
}

What type of query should I follow. My final task before going into project. When I know this will start my life as a developer. Any suggestions Please.

+2  A: 

Do you mean a HQL query? Well, a typical query on your Leavetable entity would looks like this:

Query q = session.createQuery("from Leavetable t where t.someField = :value");
q.setParameter("value", foo);
List<Leavetable> results = q.list();

However, if you just want to retrieve an entity by identifier, see Session#load() or Session#get(). I don't want to make things too much confusing but while both methods are similar, there is an important difference between both of them. Quoting the Hibernate Forums:

Retrieving objects by identifier

The following Hibernate code snippet retrieves a User object from the database:

User user = (User) session.get(User.class, userID);

The get() method is special because the identifier uniquely identifies a single instance of a class. Hence it’s common for applications to use the identifier as a convenient handle to a persistent object. Retrieval by identifier can use the cache when retrieving an object, avoiding a database hit if the object is already cached. Hibernate also provides a load() method:

User user = (User) session.load(User.class, userID);

The load() method is older; get() was added to Hibernate’s API due to user request. The difference is trivial:

If load() can’t find the object in the cache or database, an exception is thrown. The load() method never returns null. The get() method returns null if the object can’t be found.

The load() method may return a proxy instead of a real persistent instance. A proxy is a placeholder that triggers the loading of the real object when it’s accessed for the first time; we discuss proxies later in this section. On the other hand, get() never returns a proxy.

Choosing between get() and load() is easy: If you’re certain the persistent object exists, and nonexistence would be considered exceptional, load() is a good option. If you aren’t certain there is a persistent instance with the given identifier, use get() and test the return value to see if it’s null. Using load() has a further implication: The application may retrieve a valid reference (a proxy) to a persistent instance without hitting the database to retrieve its persistent state. So load() might not throw an exception when it doesn’t find the persistent object in the cache or database; the exception would be thrown later, when the proxy is accessed.

Of course, retrieving an object by identifier isn’t as flexible as using arbitrary queries.

See also the Hibernate Documentation (links below).

Reference

Pascal Thivent
@pascal thanks a lot, can i write my Hql query like thisQuery q = session.createQuery("from Leavetable t where t.leaveID = :id");q.setParameter("id", ...); what does that foo denote hear
Code 'N' Weed
@pascal For updation i dont need a query itseems, hibernate does that for me. session.saveOrUpdate( leave ). am i right
Code 'N' Weed
@Code That foo was just a random example because I don't know what a Leavetable looks like :) But I have added more details about the way to retrieve an object by its identifier since I understand now that this might have been your initial intention (vs "generic" query).
Pascal Thivent
@thanks a lot for making me understand the two methods and their difference.
Code 'N' Weed
@ya i found it,foo is the id value which am passing through my method.
Code 'N' Weed
@Code You're welcome. Good luck with your life as developer.
Pascal Thivent
@Code `foo` is the variable holding the value that is set in the `:value` "named parameter" in the hql query.
Pascal Thivent
@pascal thank you very much. I will keep you all as my mentors and definitely will succeed. Thank you very much for your kind support.
Code 'N' Weed