Which is the best method(performance wise) to delete an object if only its id is available.
-
HQL. Will executing this HQL load the SessionContext object into hibernate persistence context ?
for(int i=0; i<listOfIds.size(); i++){ Query q = createQuery("delete from session_context where id = :id "); q.setLong("id", id); q.executeUpdate(); } -
Load by ID and delete.
for(int i=0; i<listOfIds.size(); i++){ SessionContext session_context = (SessionContext)getHibernateTemplate().load(SessionContext.class, listOfIds.get(i)); getHibernateTemplate().delete(session_context) ; }
Here SessionContext is the object mapped to session_context table.
Or, well off course is there an all together different and better approach ?
Out of the two, the first one is better, where you will save memory. When you want to delete the Entity and you have the id with you, writing a HQL is preferred.
In you case there is a third and better option,
Try the below,
Now if there are 4 items in the list only one query will be fired, Previously there would be 4.
Note:- For the above to work,
session_contextshould be an independent table.