Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Bulk Delete

  Asked By: Bakir    Date: Jul 24    Category: Java    Views: 661
  

I want to execute an hql:
"delete from LoanedItem loanedItem where loanedItem.id=?"
but I have an exception as follows:
"query must begin"

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Timothy Patterson     Answered On: Jul 24

in HQL you can't use table aliases for update/delete

 
Answer #2    Answered By: Jezza Brown     Answered On: Jul 24

you must use executeUpdate instead of execute  or excecuteQuery as following code:


Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

String hqlInsert = "insert into DelinquentAccount (id, name) select c.id, c.name from Customer c where ...";
int createdEntities = s.createQuery( hqlInsert )
.executeUpdate();
tx.commit();
session.close();

 
Answer #3    Answered By: Norman Ray     Answered On: Jul 24

Simple example of delete  HQL is below :

Session session = HibernateUtil.currentSession();

String hql  = "delete from Product where name = :name";
 query  query = session.createQuery(hql);
query.setString("name","Product 1");
int rowCount = query.executeUpdate();
System.out.println("Rows affected: " + rowCount);

 
Didn't find what you were looking for? Find more on Bulk Delete Or get search suggestion and latest updates.




Tagged: