2009-12-28 69 views
1

您可以使用条件查询删除吗?使用条件查询删除

我想做的事:

DELETE bookmars 其中userid = @userID

我怎样才能做到这一点与标准是什么?

回答

-1

创建一个Java类:

下面是我们的Java文件(DeleteHQLExample.java)的代码,我们将从保险表使用查询delete from Insurance insurance where id = 2

删除行

以下是删除查询的代码: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()); 
    } 
    } 
}