can you delete using criteria query?
I want to do:
DELETE bookmars WHERE userID = @userID
How can I do this with criteria?
can you delete using criteria query?
I want to do:
DELETE bookmars WHERE userID = @userID
How can I do this with criteria?
Check out this SO question how-can-one-delete-nhibernate-objects-using-a-criteria
Looks horrible though!
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: http://stackoverflow.com/questions/1326950/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
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());
}
}
}