Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Hibernate session object's delete method is not working?

Tags:

java

hibernate

Why session object's delete method is not working in GenericDAOImpl.java, neither its giving any exception nor its showing any output. All other methods working fine expect public void delete(T object), Please help me, Sorry if i asked this question in wrong way.

public class GenericDAOImpl<T> implements IGenericDAO<T> {
private SessionFactory sessionFactory;

public GenericDAOImpl(Class<T> cl, SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
    if (sessionFactory == null)
        throw new RuntimeException("Session factory is null!!!");
}

@Override
public T get(Class<T> cl, Long id) {
    Session session = sessionFactory.getCurrentSession();
    session.beginTransaction();
    @SuppressWarnings("unchecked")
    T element = (T) session.get(cl, id);
    session.getTransaction().commit();
    return element;
}

@Override
public T get(Class<T> cl, Serializable obj) {
    Session session = sessionFactory.getCurrentSession();
    session.beginTransaction();     
    @SuppressWarnings("unchecked")   
    T element = (T) session.get(cl, obj);
    session.getTransaction().commit();
    return element;
}

@Override
public T save(T object) {
    Session session = sessionFactory.getCurrentSession();
    session.beginTransaction();
    session.save(object);
    session.getTransaction().commit();
    return object;
}

@Override
public void update(T object) {
    Session session = sessionFactory.getCurrentSession();
    session.beginTransaction();
    session.update(object);
    session.getTransaction().commit();
}

@Override
public void delete(T object) {
    Session session = sessionFactory.getCurrentSession();
    session.beginTransaction();
    session.delete(object);
    session.getTransaction().commit();
}

@SuppressWarnings("unchecked")
@Override   
public T findUniqueByQuery(String hsql, Map<String, Object> params) {
    Session session = sessionFactory.getCurrentSession();
    session.beginTransaction();
    Query query = session.createQuery(hsql);
    if (params != null) {
        for (String i : params.keySet()) {
            query.setParameter(i, params.get(i));
        }
    }
    return (T) query.uniqueResult();
}

@SuppressWarnings("unchecked")
@Override
public List<T> query(String hsql, Map<String, Object> params) {
    Session session = sessionFactory.getCurrentSession();
    session.beginTransaction();
    Query query = session.createQuery(hsql);
    if (params != null) {
        for (String i : params.keySet()) {
            query.setParameter(i, params.get(i));
        }
    }

    List<T> result = null;
    if ((hsql.toUpperCase().indexOf("DELETE") == -1)
            && (hsql.toUpperCase().indexOf("UPDATE") == -1)
            && (hsql.toUpperCase().indexOf("INSERT") == -1)) {
        result = query.list();
    } else {
    }
    session.getTransaction().commit();

    return result;
    }

}
like image 484
Praveen Rawat Avatar asked Sep 29 '15 16:09

Praveen Rawat


People also ask

How do I delete a Session in Hibernate?

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.

What is Session delete?

Description ¶ session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie. To use the session variables again, session_start() has to be called.

When Session factory object is used in Hibernate?

Session object is created based upon SessionFactory object i.e. factory. It opens the Connection/Session with Database software through Hibernate Framework. It is a light-weight object and it is not thread-safe. Session object is used to perform CRUD operations.

What is Session SessionFactory and transaction in Hibernate?

SessionFactory is a factory class for Session objects. It is available for the whole application while a Session is only available for particular transaction. Session is short-lived while SessionFactory objects are long-lived. SessionFactory provides a second level cache and Session provides a first level cache.


1 Answers

As investigated in comments, you are facing

org.hibernate.TransactionException: nested transactions not supported exception

This is happening because you began transaction and never committed or rollbacked upon an exception.

I can see one of it's case in your code. See your code below

@SuppressWarnings("unchecked")

@Override   
public T findUniqueByQuery(String hsql, Map<String, Object> params) {
    Session session = sessionFactory.getCurrentSession();
    session.beginTransaction();
    Query query = session.createQuery(hsql);
    if (params != null) {
        for (String i : params.keySet()) {
            query.setParameter(i, params.get(i));
        }
    }
    return (T) query.uniqueResult();
}

See, you began and never committed a transaction. Like wise check all other places in your project.

like image 62
Suresh Atta Avatar answered Sep 25 '22 22:09

Suresh Atta