1

can you delete using criteria query?

I want to do:

DELETE bookmars WHERE userID = @userID

How can I do this with criteria?

mrblah
  • 99,669
  • 140
  • 310
  • 420

2 Answers2

4

I'm not sure you can do anything other than query with Criteria, though there is an example of using Criteria to delete already posted on StackOverflow: How can one delete NHibernate objects using a criteria?

I take it you just want to delete without having to return data. You might try HQL instead: http://nhibernate.sourceforge.net/NHibernateEg/NHibernateEg.Tutorial1A.html#NHibernateEg.Tutorial1A-CRUD.Delete

Community
  • 1
  • 1
Chris Nicola
  • 14,384
  • 6
  • 47
  • 61
-1

Create a java class:

Here is the code of our java file (DeleteHQLExample.java), which we will delete a row from the insurance table using the query delete from Insurance insurance where id = 2

Here is the code of delete query: DeleteHQLExample.java

package roseindia.tutorial.hibernate;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class DeleteHQLExample {
  /**
 * @author ashish baviskar
 */
  public static void main(String[] args) {
    // TODO Auto-generated method stub  
    Session sess = null;
    try {
      SessionFactory fact = new 
Configuration().configure().buildSessionFactory();
      sess = fact.openSession();
      String hql = "delete from 
Insurance insurance where id = 2";
      Query query = sess.createQuery(hql);
      int row = query.executeUpdate();
      if (row == 0){
        System.out.println("Doesn'
t deleted any row!");
      }
      else{
        System.out.println("Deleted
 Row: " + row);
      }
      sess.close();
    }
    catch(Exception e){
      System.out.println(e.getMessage());
    }
  }
}
Joel
  • 7,401
  • 4
  • 52
  • 58
Ashish
  • 183
  • 3
  • 4
  • 12