Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
david.karr - 26 Mar 2007 23:21 GMT
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 - 26 Mar 2007 23:32 GMT
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 - 26 Mar 2007 23:50 GMT
> hi-
>
[quoted text clipped - 13 lines]
> 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?
3rdshiftcoder - 27 Mar 2007 00:15 GMT
> 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 thirdshiftcoder@yahoo.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 - 27 Mar 2007 00:17 GMT
> 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 thirdshiftcoder@yahoo.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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.