• 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

Hibernate delete

 
Ranch Hand
Posts: 193
Mac OS X Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's say I have 1000 records in the database already, with IDs automatically generated by Hibernate incrementally when the objects being persisted into the database. However, there is no guarantee that the first record has an ID=1, it could be any numeric value.

Now for example if I want to delete the first 400 records, how should I address this operation in Hibernate in an effective fashion?

Thanks,
Jiafan
 
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you can use BETWEEN Operator in your SQL like



Hope this helps
[ September 28, 2007: Message edited by: Sheethal Reddy ]
 
Jiafan Zhou
Ranch Hand
Posts: 193
Mac OS X Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sheethal,

Thanks for you reply first. I am a question related to your solution:

How can we determine the database ID for the first record?

maybe we shall use the min() function in the database, assuming that the minimum ID will always be the first record in the database.

Hence, we need to get the minimum ID from the table, then work out how many records we are going to delete, does that sound a possible solution to you?

Regards,
Jiafan
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well here is the thing, you don't know if the 400th record id is 400 or 546 or 426 or 476, because they might have skipped to. So you probably will have to find out if there is a "TOP" type query in the database that you are using, or you are going to have to do some kind of other query or looping through the record to figure out what the ID is for the 400th record.

Mark
 
Jiafan Zhou
Ranch Hand
Posts: 193
Mac OS X Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark,

The object model has a constraint which is an individual object(record) deletion is not allowed and supported. Hence the circumstance you described will never appear unless Hibernate does not assign ID incrementally to records.

Furthermore , I think looping through all the records is a very expensive solution in the database. I would agree with Sheethal suggustion to use a native SQL query as "delete from ... between".

I am pretty surre there is better solutions.

Regards,
Jiafan
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So are you saying that you can say 100% without a doubt what the id of the 400th record is, when Hibernate isn't starting at 1?

If so, then between sounds good to me.

Mark
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic