Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

is better to load instance before delete it?

 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I load instances before delete it, I mean:
MyClass instance = dao.read(id);
dao.delete(instance);

But I saw an example that he simply do this:
dao.delete(new MyClass(id));

It's simpler, and it has better performance. My doubt is if there is some problem using this approach.
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure if you friend's code will work.


You need to retrieve the persistent object first. If you use session.load(), you will only get a proxy object and thus no database hit will occur. Then use that proxy object to delete the instance.
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!