Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate delete

 
Jiafan Zhou
Ranch Hand
Posts: 193
Fedora Linux Mac OS X
  • Mark post as helpful
  • send pies
  • 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
 
Saathvik Reddy
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • 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
Fedora Linux Mac OS X
  • Mark post as helpful
  • send pies
  • 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
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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
Fedora Linux Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic