Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Session.delete() an object if exists

In JavaDoc of Session class the description of delete method is:

Remove a persistent instance from the datastore. The argument may be an instance associated with the receiving Session or a transient instance with an identifier associated with existing persistent state.

My questions are:

  1. I want to delete a detach object, can I use this method, AFAIK session first makes an object persistent from detach and then perform its operation. Am I right?
  2. If I am not sure about the existence of the object in database, should I use Session.get() to check the whether or not it is null and then perform delete operation or I can use directly delete operation?

Here is a code snippet:

public void removeUnallocatedUserIfExits(final long itemId) {
    getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            session.flush();
            session.setCacheMode(CacheMode.IGNORE);
            UnallocatedUser unallocatedUser;
            if ((unallocatedUser = (UnallocatedUser) session.get(UnallocatedUser.class, itemId)) != null) {
                session.delete(unallocatedUser);
            }
            session.flush();
            return null;
        }
    });
}

Is the okay?

like image 988
Tapas Bose Avatar asked Apr 25 '11 07:04

Tapas Bose


People also ask

What is Session delete in Hibernate?

In JavaDoc of Session class the description of delete method is: Remove a persistent instance from the datastore. The argument may be an instance associated with the receiving Session or a transient instance with an identifier associated with existing persistent state.

How do I remove an object from a Session?

In Hibernate, an entity can be removed from a database by calling the Session#delete() or Session#remove() . Using these methods, we can remove a transient or persistent object from datastore. The following examples demonstrate the use of Session#delete() and Session#remove() methods.

How does saveOrUpdate work in Hibernate?

saveOrUpdate() Hibernate will check if the object is transient (it has no identifier property) and if so it will make it persistent by generating it the identifier and assigning it to session. If the object has an identifier already it will perform .

What is true about the Session evict () method?

The method evict() removes a single object from Session cache in ORM (Object Relationship Mapping) framework Hibernate. So before you call evict() the object should be there in the Session cache. Therefore if you save the object first time, you have to save the object via Session.


1 Answers

or a transient instance with an identifier associated with existing persistent state

This means you can directly pass your entity to session.delete(), in order to delete that object. Further, you need not check whether the entity exist or not. There should be an exception if no record found in the database. In fact, we usually don't really get this case. We always delete an existing entity, I mean usual logic is like that; so, no need to do that. You can simply do this,

SomeEntity ent = session.load(SomeEntity.class, '1234');
session.delete(ent);

or you can do this instead,

SomeEntity ent = new SomeEntity('1234'); // used constructor for brevity
session.delete(ent);

Btw, you can also use this version session.delete(String query),

sess.delete("from Employee e where e.id = '1234'"); // Just found it is deprecated
like image 88
Adeel Ansari Avatar answered Sep 28 '22 15:09

Adeel Ansari