• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate delete performance

 
Bob Lawson
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an aggregation of objects, where one object contains many of another, each of which contain many of another, etc, etc. Here is my annotation for all one-to-many relationships:


@OneToOne(mappedBy="iterationCounty", fetch = FetchType.LAZY, cascade = { CascadeType.ALL })


The problem is that there are thousands of records to delete from various related tables, and it is taking forever. It seems that hibernate is actually loading in all the objects and then deleting them.

How can I improve performance?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do the delete in HQL instead.
 
Bob Lawson
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was able to solve this problem by setting the hibernate cascade type to SaveOrUpdate, and then setting the cascade delete option in the database. It screams now.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob Lawson wrote:I was able to solve this problem by setting the hibernate cascade type to SaveOrUpdate, and then setting the cascade delete option in the database. It screams now.


Be aware not all databases support cascade deletes so your application will not work on those DBs.
 
Bob Lawson
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there a better way?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Avoiding database specifics, your only option is a delete in HQL (mentioned before). Applications that use ORMs all need tuned in some way or other, and no ORM is good at batch operations. If you know you are only using one database implementing it there is probably fine. If you are unsure what other platforms you need to support, I would consider an HQL delete statement.
 
Bob Lawson
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good to know what my options are. Thanks for the info!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic