Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Hibernate305: delete query fails with "must begin with SELECT or FROM"

Reply
Thread Tools

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

 
 
david.karr
Guest
Posts: n/a
 
      03-26-2007
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?

 
Reply With Quote
 
 
 
 
3rdshiftcoder
Guest
Posts: n/a
 
      03-26-2007
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();
}
}
}


 
Reply With Quote
 
 
 
 
david.karr
Guest
Posts: n/a
 
      03-26-2007
On Mar 26, 3:32 pm, "3rdshiftcoder" <(E-Mail Removed)> 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?

 
Reply With Quote
 
3rdshiftcoder
Guest
Posts: n/a
 
      03-26-2007

> 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 http://www.velocityreviews.com/forums/(E-Mail 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




 
Reply With Quote
 
3rdshiftcoder
Guest
Posts: n/a
 
      03-26-2007

> 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 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





 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
count_if(somestr.begin(), somestr.end(), isalpha) fails to compile ongcc, but compiles on VS2008 rep_movsd C++ 15 08-17-2009 11:50 AM
Force Download fails when I select "Open" but works fine when I select "Save" Michael ASP General 0 01-25-2005 08:58 PM
slideshow fails, Firefox debugger also fails lkrubner@geocities.com Javascript 2 12-23-2004 06:22 PM
select of select box will select multiple in another box palmiere Javascript 1 02-09-2004 01:11 PM
Forms Authentication Fails Between ASP.NET 1.0 and 1.1 Applications (Cookie Decryption Fails) John Saunders ASP .Net 1 11-18-2003 03:25 PM



Advertisments