Java Tutorial/Hibernate/Delete

Материал из Java эксперт
Перейти к: навигация, поиск

HSQL Delete With Parameter

File: Main.java



import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
public class Main {
  public static void main(String[] args) throws Exception {
    HibernateUtil hibernateUtil = new HibernateUtil();
    hibernateUtil
        .executeSQLCommand("create table survey (id int,name varchar, purchasedate date);");
    Session session = hibernateUtil.getSession();
    Survey survey = new Survey();
    survey.setName("Survey");
    survey.setPurchaseDate(new Date());
    session.save(survey);
    
    survey = new Survey();
    survey.setName("Survey1");
    survey.setPurchaseDate(new Date());    
    session.save(survey);
    
    session.flush();
    
    String hql = "delete from Survey where name = :name";
    Query query = session.createQuery(hql);
    query.setString("name","Survey");
    int rowCount = query.executeUpdate();
    System.out.println("Rows affected: " + rowCount);

    
    session.close();
    hibernateUtil.checkData("select * from survey");
  }
}