3

I use the following code to delete a record from table. I was able to successfully insert data to that table. But this delete operation does not work.

 @Override
    public int deleteButterflyCountData(int recordId) {
        Session session = this.sessionFactory.openSession();
        session.beginTransaction();
        String query = "delete FROM ButterflyCountData where record_id="+recordId+"";
        Query q = session.createQuery(query);
        int result = q.executeUpdate();
        session.close();
        return result;
    }

result also returns 1 but data table record is not be deleted. What is the issue in here?

2
  • I don't know how your getting result. Your query have syntax error... FORM is not valid with delete query... If you getting result then commit your session before close.... Commented Jul 13, 2015 at 15:41
  • He is not using FORM Commented Jul 13, 2015 at 15:48

3 Answers 3

4

You have to commit the transaction:

Transaction transaction = session.beginTransaction();
...
transaction.commit();
session.close();
Sign up to request clarification or add additional context in comments.

Comments

2
 HQL Delete Query Example
Delete a stock where stock code is “7277”.

Query query = session.createQuery("delete Stock where stockCode = :stockCode");
query.setParameter("stockCode", "7277");

int result = query.executeUpdate();


Query query = session.createQuery("delete Stock where stockCode = '7277'");

int result = query.executeUpdate();

2 Comments

Tried in all ways. Still has the same problem
On top of syntax error you are not committing transaction if you ever get that far
0

I replaced :

@Transactional
public void deleteCustomer(Long customerId) {
    Session currentSession = sessionFactory.getCurrentSession();
    Query<Customer> query= currentSession
            .createQuery("DELETE FROM Customer c where c.id =:customerId", Customer.class);
    query.setParameter("customerId", customerId);
    query.executeUpdate();
}

with: I also had the same issue with hibernate 6. You can do this way even if it adds a query:

@Transactional
public void deleteCustomer(Long customerId) {
    Session currentSession = sessionFactory.getCurrentSession();
    currentSession.remove(currentSession.get(Customer.class, customerId));
}

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.