wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Hql delete syntax? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hql delete syntax?" Watch "Hql delete syntax?" New topic
Author

Hql delete syntax?

Ian Su
Greenhorn

Joined: Nov 18, 2010
Posts: 25
I have a table named 'insurance'.Column names are ID,insurance_name,invested_amount,investment_date.
Mapping object class to 'insurance' is Insurance.java.It has the required methods(setter,getter) to bind with the 'insurance' table and the instance variables are lngInsuranceId,insuranceName,investmentAmount,investmentDate.
I would like to delete a row from 'insurance' table.So I have a test class to delete a row from 'insurance' table.The test class is


My hibernate mapping configuration is also ok.
I am a new to hibernate.And what I would like to know is whether the delete hql is correct or not.It may be wrong because it just shows null when I executes in eclipse platform.
I hopes to overcome this problem with your help.Thank you very much.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

Hi Ian, try this


You can refer this link.


Love all, trust a few, do wrong to none.
Ian Su
Greenhorn

Joined: Nov 18, 2010
Posts: 25
Thank you very much chaitanya karthikk.Your advice really helps me.And Do you know the way to delete a row that I mentioned above?
And I would like to know also something.It is when the delete operation is successful,it will delete the row with specific id(Primary key & auto_increment').
Eg.I have a table with 3 rows and These 3 records have id field filled with int data(auto_increment).That is 1,2,3.What I would like to do is when I delete the 2nd row and the 3rd row id will automatically reassigned to the above row id(the deleted row id).The 3rd row id will be 2 and like that.How to do it?I need some advice from you all.Thank you very much.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

Ian Su wrote:Eg.I have a table with 3 rows and These 3 records have id field filled with int data(auto_increment).That is 1,2,3.What I would like to do is when I delete the 2nd row and the 3rd row id will automatically reassigned to the above row id(the deleted row id).The 3rd row id will be 2 and like that.How to do it?I need some advice from you all.Thank you very much.


Hi Ian Su, this idea is not a good one. Take an instance, suppose there is another table referring to the table which you are talking about. If you try to update the parent row, you will get an error. So don't try to do this because there might occur many problems.
Ian Su
Greenhorn

Joined: Nov 18, 2010
Posts: 25
Thank you chaitanya karthikk.You are right.
Yesterday,I posted this topic and you gave me a good answer and I overcame my problem.And let me say,just take a look at my original coding and do you think there is anything wrong at hql delete statement because I am just a hibernate starter?I just run it and get null return.I don't just get my expected return.Thank you chaitanya karthikk and everyone.

Even if I correct to this :
It is still the same.How do I do?Can I approach in this way?
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

Hi Ian, I am also a hibernate beginner. I dint try to delete a record using hql query. Anyhow I will try and tell you.
Hemant Thard
Ranch Hand

Joined: Dec 23, 2008
Posts: 122
Hi Ian,

yes, your delete query is wrong.

There are 2 thing i will like to advice you on your original post.

1. Always use transaction for any DML operation you perform. Otherwise the same would not be reflected on the database.
2. Since we are using ORM tool like hibernate, always work with object.
so the same query that you have written can be re-written as :



Hope this Helps,

Hemant
Ian Su
Greenhorn

Joined: Nov 18, 2010
Posts: 25
Hi chaitanya & Hemant,
Your answers and advice are really appreciated.But the last one Hemant advises me :


still doesn't work.I am still finding that answer and Thank you everyone for helping me.
Waswani Naresh
Ranch Hand

Joined: May 01, 2008
Posts: 66
Hi Ian,

Try this.



Regards,
Naresh Waswani


Naresh Waswani
Hemant Thard
Ranch Hand

Joined: Dec 23, 2008
Posts: 122
Thanks Naresh for correcting me up.

Ian, this should work for you.


Regards,
Hemant
Ian Su
Greenhorn

Joined: Nov 18, 2010
Posts: 25
Thank you Naresh & Hemant for your hand.
But for me,it is still not ok.
I will show you the detail of my works.
This is my database.

This is my mapping file.

This is my POJO file.Insurance.java


This is my testing file.DeleteHQLExample.java


When I run the testing file,it just returns null.delete sql statement is wrong or something else.
Thank you everyone.
regards,
Ian
Waswani Naresh
Ranch Hand

Joined: May 01, 2008
Posts: 66
Execute your query with in a transaction.

Regards,
Naresh
Hemant Thard
Ranch Hand

Joined: Dec 23, 2008
Posts: 122
hi Ian,

execute your query within transaction as suggested by Naresh.

Also i would recommend you to use wrapper class instead of primitive.

Click here for more detail.

Hope this Helps,
Hemant
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hql delete syntax?
 
Similar Threads
Delete Query in hibernate doesnt work
what jar file need to run query.executeUpdate()
Null pointer exception in HQL using parameter
Unable to delete
delete query in hibernate giving error as null, and not deleting the record