This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt on delete of a parent entity

 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

I have a scenario wherein I have an Entity (say for example Employee) and this entity has a OneToMany annotation applied to one of its property called Address (and it lazily fetched). Now in my screen where I display the list of employees, I select one of the employee and click delete. Now I want to acheive a scenario where in I have to check if the employee has an address and if yes, then I do not want to delete him and I show the user a message saying that there are references in the child table. Now I can do this in two ways, one is to simply try to delete it and when any references are there in the child table, I get an exception with the transaction rolled back and I show the user the appropriate message. The other way is to fetch the address property and if there are references, show a message to the user that the record cannot be deleted. Here I do not want to fetch the records as it is a OneToMany relationship and also I do not want to follow the other crude way. Is there any other option to acheive this?
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it sounds like you understand your problem, and you understand your options. I'm not sure how many more options exist?

I'm moderately curious as to why doing selects on your database strikes so much fear into your heart. Do it within an open session, and it all happens within one transactions.

-Cameron McKenzie
 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont think your performance will be affected much if you do a count(*) of the child rows for that parent...obviously i think your parent id in child will be indexed...it will be pretty fast...
But you could even try to delete the Parent and give an error if hibernate exception occurs.....however it may not work in 100% of the cases....
In the databases, you 'can' define the parent child relationship in different ways...like when the parent is deleted, the children should automatically get deleted or they should become orphan....(There are ways of defining FKs)..

So, unless you know your database constraints properly.....do a count(*) of children and then delete.
If you know your DB properly...and every performance gain is a big bonus for you....you can try a direct delete...

Needless to say, when in doubt...play safe....count and delete... :-)
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my humble opinion, it seems that great effort is being made to over-engineer a simple scenario. Several selects within a single transaction are extremely efficient. Maintaining code that does counts and queries and catches and coalates and coordinates and copulates is where the biggest inefficiencies lie.

Just keeping it simple and doing the selects seems the simplest approach, and until someone came to me and said 'this single call within a transaction is bringing our system to a hault', I'd just go with the simple solution.

But, then again, I'm lazy.

-Cameron McKenzie
 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cameron Wallace McKenzie wrote:
But, then again, I'm lazy.
-Cameron McKenzie


Are you? Doing a select and then deleting it is not being lazy, right...

the count(*) point that i made is because...if you have 100 children for a parent...and you select all of them....why?...
we only want to know even if a single record exists, dont delete the parent...so, count should be done...in my opinion...

Regarding the catching of the exception...it is just one way, and it should be done only when each query to DB is costly....

Anyways, the point is one should know the pros and cons of both the schemes so that they can choose wisely..

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic