radik
radik

Reputation: 3

Truncate/Delete From on Hibernate

Im having a problem when using DELETE FROM myEntity

I saw on my database that all the entry are gone, but when i tried to insert a previous existed entry, its still have the record from before i delete it.

Here's the code :

static SessionFactory session = NewHibernateUtil.getSessionFactory();

public Session membukaSession(){
  return session.openSession();
}

public void clearRencanaPesan(){
    Session sess = this.membukaSession();
    Query query = sess.createQuery("delete from Rencanapesan");
    query.executeUpdate();
}

So i tried using Truncate

And this is the code :

public void clearRencanaPesan(){
    Session sess = this.membukaSession();
    Query query = sess.createQuery("TRUNCATE Table Rencanapesan");
    query.executeUpdate();
}

This one isn't working at all @@, the entry isnt deleted.

this is the error

Jun 18, 2012 11:01:54 PM org.hibernate.hql.ast.ErrorCounter reportError SEVERE: line 1:1: unexpected token: TRUNCATE Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: node to traverse cannot be null!

Please help me, how can i truncate or delete all entry.
Thank you so much.

Upvotes: 0

Views: 5659

Answers (1)

jeroen
jeroen

Reputation: 38

The query you've provided contains no error. My first guess was that the session gets out of sync after manually deleting it using the query. Session.flush() makes sure the session is synchronized with the underlying database.

public void clearRencanaPesan(){
    Session sess = this.membukaSession();
    Query query = sess.createQuery("delete from Rencanapesan");
    query.executeUpdate();
}
// after you've made your changes and before closing the session.
sess.flush();

Upvotes: 2

Related Questions