Hibernate305: delete query fails with "must begin with SELECT or FROM"  
Author Message
david.karr





PostPosted: 2007-3-27 6:21:00 Top

java-programmer, Hibernate305: delete query fails with "must begin with SELECT or FROM" 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?

 
3rdshiftcoder





PostPosted: 2007-3-27 6:32:00 Top

java-programmer >> Hibernate305: delete query fails with "must begin with SELECT or FROM" 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();
}
}
}


 
david.karr





PostPosted: 2007-3-27 6:51:00 Top

java-programmer >> Hibernate305: delete query fails with "must begin with SELECT or FROM" On Mar 26, 3:32 pm, "3rdshiftcoder" <email***@***.com> wrote:
> 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?

 
 
david.karr





PostPosted: 2007-3-27 7:01:00 Top

java-programmer >> Hibernate305: delete query fails with "must begin with SELECT or FROM" (I posted this first to c.l.j.p, but I realized it's more appropriate
here.)

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?

I also tried changing "delete ReqField" to "delete from ReqField", but
that still gets the same error message (with "delete from" in the
message).

 
 
3rdshiftcoder





PostPosted: 2007-3-27 7:15:00 Top

java-programmer >> Hibernate305: delete query fails with "must begin with SELECT or FROM"
> 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 email***@***.com 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




 
 
3rdshiftcoder





PostPosted: 2007-3-27 7:17:00 Top

java-programmer >> Hibernate305: delete query fails with "must begin with SELECT or FROM"
> 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 email***@***.com 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





 
 
joeNOSPAM@BEA.com





PostPosted: 2007-3-27 8:31:00 Top

java-programmer >> Hibernate305: delete query fails with "must begin with SELECT or FROM" On Mar 26, 4:01 pm, "david.karr" <email***@***.com> wrote:
> (I posted this first to c.l.j.p, but I realized it's more appropriate
> here.)
>
> 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?
>
> I also tried changing "delete ReqField" to "delete from ReqField", but
> that still gets the same error message (with "delete from" in the
> message).

I guess you should not use a query object for an update, insert or
delete,
just queries.

 
 
david.karr





PostPosted: 2007-3-27 10:08:00 Top

java-programmer >> Hibernate305: delete query fails with "must begin with SELECT or FROM" On Mar 26, 5:31 pm, "email***@***.com" <email***@***.com>
wrote:
> On Mar 26, 4:01 pm, "david.karr" <email***@***.com> wrote:
>
>
>
> > (I posted this first to c.l.j.p, but I realized it's more appropriate
> > here.)
>
> > 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?
>
> > I also tried changing "delete ReqField" to "delete from ReqField", but
> > that still gets the same error message (with "delete from" in the
> > message).
>
> I guess you should not use a query object for an update, insert or
> delete,
> just queries.

So how can you delete rows from a query? I tried the "deprecated"
form of "Session.delete(querystr)", but that doesn't work either (I
can't remember what failed there).

I guess I'll have to make sure the session is not in auto-commit and
iterate through all the objects manually, calling "delete()" on them.
I hope that will work.