Hql delete syntax?

Ian Su

Greenhorn

Posts: 25

posted 5 years ago

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.

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.

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.

Love all, trust a few, do wrong to none.

Ian Su

Greenhorn

Posts: 25

posted 5 years ago

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?

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.

Love all, trust a few, do wrong to none.

Hemant Thard

Ranch Hand

Posts: 122

posted 5 years ago

1

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 :