• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hql delete syntax?

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ian, try this


You can refer this link.
 
Ian Su
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 122
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ian,

Try this.



Regards,
Naresh Waswani
 
Hemant Thard
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Naresh for correcting me up.

Ian, this should work for you.


Regards,
Hemant
 
Ian Su
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Execute your query with in a transaction.

Regards,
Naresh
 
Hemant Thard
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic