Hibernate305: delete query fails with "must begin with SELECT or FROM"

D

david.karr

I'm using Hibernate 3.0.5, JDK 1.4.2, and Oracle 9i.

I'm following the Hibernate 3.0.5 docs to build a query to delete rows
from a table. My code looks something like this:

-----------------
String hql = "delete ReqField " +
"where fieldName = :fieldName and value
= :value";
Query query =
sessionFactory.getCurrentSession().createQuery(hql);
query.setString("fieldName", fieldName);
query.setString("value", value);

int deletedRows = query.executeUpdate();
-----------------

This fails with:

org.hibernate.QueryException: query must begin with SELECT or FROM:
delete [delete ReqField where fieldName = :fieldName and value
= :value]

What is wrong with my query?
 
3

3rdshiftcoder

hi-

i use hibernate and the newsgroup over their had some sort of merit system.
i dont have much merit to offer but here is some code from my program
i am working on that might help you see what you did wrong.
this delete query works.

hope it helps,
jim

private void deleteRecord(){
IStructuredSelection selection =
(IStructuredSelection)tableViewer.getSelection();
Register register = (Register)selection.getFirstElement();
if (register == null) {
System.out.println("Please select an item first. ");
return;
}
MessageBox messageBox = new MessageBox(shell, SWT.YES | SWT.NO);
messageBox.setText("Confirmation");
messageBox.setMessage(
"Are you sure to remove the bug with id #"
+ register.transactionid);
if (messageBox.open() == SWT.YES) {
//register.remove(register);
Session session =
HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Query query = session.createQuery("delete from Transact where transactionid
= ?");
query.setLong(0, Long.valueOf(register.transactionid));
int count = query.executeUpdate();
tx.commit();
session.flush();
bugs.remove(table.getSelectionIndex());
tableViewer.setInput(bugs);
tableViewer.refresh();
} catch (HibernateException e) {
if (tx != null) tx.rollback();
displayErrorMsg(e);
} catch(Exception e1){
System.out.println("Error: " + e1.getMessage());
displayErrorMsg(e1);
}finally {
session.close();
}
}
}
 
D

david.karr

hi-

i use hibernate and the newsgroup over their had some sort of merit system.
i dont have much merit to offer but here is some code from my program
i am working on that might help you see what you did wrong.
this delete query works.

hope it helps,
jim

[deleted]
tx = session.beginTransaction();
Query query = session.createQuery("delete from Transact where transactionid
= ?");
query.setLong(0, Long.valueOf(register.transactionid));
int count = query.executeUpdate();
tx.commit();

I don't get it. I changed "delete" in my query to "delete from", but
it still gives me the same error message (although it now shows "from"
in my query.

Are you using Hibernate 3.0.5?
 
3

3rdshiftcoder

I don't get it. I changed "delete" in my query to "delete from", but
it still gives me the same error message (although it now shows "from"
in my query.

Are you using Hibernate 3.0.5?

no. i am using Hibernate 3.2.0beta8.
i use pojos with annotations.

delete [delete ReqField where fieldName = :fieldName and value
= :value]
String hql = "delete ReqField " +
"where fieldName = :fieldName and value
= :value";

it has to be delete from TABLE instead of delete from ReqField
or really the representation of the table (the related pojo object).
i think ReqField might be a typo and i apologize if that is the case.

if you have any more questions, i can try to help but contact me
at (e-mail address removed) as this is off-topic for the group.
having said that, i hope someone in the group offers to help you here
even though it is off topic. i am not a hibernate expert. not even close.

later
 
3

3rdshiftcoder

I don't get it. I changed "delete" in my query to "delete from", but
it still gives me the same error message (although it now shows "from"
in my query.

Are you using Hibernate 3.0.5?

no. i am using Hibernate 3.2.0beta8.
i use pojos with annotations.

delete [delete ReqField where fieldName = :fieldName and value
= :value]
String hql = "delete ReqField " +
"where fieldName = :fieldName and value
= :value";

it has to be delete from TABLE instead of delete from ReqField
or really the representation of the table (the related pojo object).
i think ReqField might be a typo and i apologize if that is the case.

if you have any more questions, i can try to help but contact me
at (e-mail address removed) as this is off-topic for the group.
having said that, i hope someone in the group offers to help you here
even though it is off topic. i am not a hibernate expert. not even close.

later
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top