5

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;
    }

}
7
  • 1
    Sorround with try catch and see is there any exception Commented Sep 29, 2015 at 16:22
  • How do you know it doesn't work? Can you show a test? Commented Sep 29, 2015 at 16:28
  • thanks @sᴜʀᴇsʜᴀᴛᴛᴀ now its giving org.hibernate.TransactionException: nested transactions not supported exception, but why ? Commented Sep 29, 2015 at 16:29
  • @Andres yeah i tested it Commented Sep 29, 2015 at 16:33
  • 1
    Before beginTransaction() check if check if there is already an open transaction. Commented Sep 29, 2015 at 16:59

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks @SURESH you are right, when i made this changes T result = (T) query.uniqueResult(); session.getTransaction().commit(); return result; then delete method is also working fine, thanks again bro :)
@PraveenRawat Glad to help Praveen.
2

I have the same problem. Although I was not using transaction at all. I was using namedQuery like this :

Query query = session.getNamedQuery(EmployeeNQ.DELETE_EMPLOYEES);
int rows = query.executeUpdate();
session.close();

It was returning 2 rows but the database still had all the records. Then wrap up the above code with this :

Transaction transaction = session.beginTransaction();
Query query = session.getNamedQuery(EmployeeNQ.DELETE_EMPLOYEES);
int rows = query.executeUpdate();
transaction.commit();
session.close();

Then it started working fine. I was using SQL server. But I think if we use h2 above code (without transaction) will also work fine.

Comments

1
org.hibernate.TransactionException: nested transactions not supported exception

Most probably you're not closing your session after an update or insert, and then you're doing the delete.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.