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

Discussion in 'Java' started by david.karr, Mar 26, 2007.

  1. david.karr

    david.karr Guest

    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?
     
    david.karr, Mar 26, 2007
    #1
    1. Advertising

  2. 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();
    }
    }
    }
     
    3rdshiftcoder, Mar 26, 2007
    #2
    1. Advertising

  3. david.karr

    david.karr Guest

    On Mar 26, 3:32 pm, "3rdshiftcoder" <> 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, Mar 26, 2007
    #3

  4. > 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 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, Mar 27, 2007
    #4

  5. > 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 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, Mar 27, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Saunders
    Replies:
    1
    Views:
    691
    John Saunders
    Nov 18, 2003
  2. =?Utf-8?B?T2xpdmllciBHSUw=?=

    Directory.Delete of a non empty dir fails in ASP.NET application

    =?Utf-8?B?T2xpdmllciBHSUw=?=, Oct 26, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    444
    Walter Wang [MSFT]
    Nov 10, 2006
  3. david.karr
    Replies:
    1
    Views:
    349
    david.karr
    Sep 7, 2007
  4. Replies:
    2
    Views:
    306
    Grant Wagner
    Dec 23, 2004
  5. dayo
    Replies:
    11
    Views:
    357
    Ilya Zakharevich
    Dec 16, 2005
Loading...

Share This Page