• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate, check dependancy before delete.

 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a question on hibernate. Right now I have, of course, classes that contain other classes. There are cases that I want to delete a class, but I want to make sure nothing is 'pointing' to it first. example (not a real one)

lets say I have Several different Objects: Car, Motorcycle, Truck with no relation.
They each contain an engine class. Now I want to delete an engine from the system, but I want to make sure there are no Car's, Motorcycle's, or Truck's that use that engine.

In this example right now I would be running three queries (from Car where Car.engine.id='theidhere', ect...) and looking to see if there are any results returned.

My question is, is there a better/faster/easier way to do this type of check?
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tend to prefer to create foreign key constraints and let the database enforce relational integrity. First, it avoids race conditions (you query, find no links, someone adds link, you delete, failure). Second, in the case where there are no links, you save wasteful queries. And third, you already have integrity constraints (you do, don't you?), so you may as well use them.

I'm not aware of any Hibernate support for this, so that's what I've used in porting our EJB application.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic