aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate delete Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate delete" Watch "Hibernate delete" New topic
Author

Hibernate delete

Jiafan Zhou
Ranch Hand

Joined: Sep 28, 2005
Posts: 192

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


SCJP, SCJD, SCWCD, SCBCD, SCEA
Saathvik Reddy
Ranch Hand

Joined: Jun 03, 2005
Posts: 228
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

Joined: Sep 28, 2005
Posts: 192

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

Joined: Feb 05, 2001
Posts: 17250
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Jiafan Zhou
Ranch Hand

Joined: Sep 28, 2005
Posts: 192

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

Joined: Feb 05, 2001
Posts: 17250
    
    6

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate delete