*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Why EntityManager.refresh, and EntityManager.remove require attached entity? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Why EntityManager.refresh, and EntityManager.remove require attached entity?" Watch "Why EntityManager.refresh, and EntityManager.remove require attached entity?" New topic
Author

Why EntityManager.refresh, and EntityManager.remove require attached entity?

Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Why don't they make the entity be attached if needed? Is there any design issues prevent that?

I think it doesn't make sense that if I want to refresh, or remove I need to merge first, and merging is updating, right?
Moreover, I think it's very confusing that to refresh or remove I need to call merge *to update the entity first*( before I can refresh (reset) or remove it.

There is no point to merge (update) if I want to refresh or remove an entity.


SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
jim li
Ranch Hand

Joined: May 20, 2008
Posts: 180
for entitymanager.refresh, you can retached an entity which was detached

for entitymanger.remove, you have to make sure the entity must be attached before you remove it
Deepika Joshi
Ranch Hand

Joined: Feb 24, 2009
Posts: 268
Why don't they make the entity be attached if needed? Is there any design issues prevent that?

It won't be tough for container provider to implement it. But container provider (entity manager implementor) need to follow what spec says to them.
I think it doesn't make sense that if I want to refresh, or remove I need to merge first, and merging is updating, right? Moreover, I think it's very confusing that to refresh or remove I need to call merge *to update the entity first*( before I can refresh (reset) or remove it.
There is no point to merge (update) if I want to refresh or remove an entity.

I had same doubt in this thread, and I was answer by Ralph Jaus that requirement is entity should be attached before refresh/ remove is called and that is possible by calling find() before refresh/ remove is called
em.refresh(em.find(Item.class, item.id));




Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
jim li wrote:for entitymanager.refresh, you can retached an entity which was detached

for entitymanger.remove, you have to make sure the entity must be attached before you remove it

I know, jim, and my question is "why"
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Deepika Joshi wrote:
Why don't they make the entity be attached if needed? Is there any design issues prevent that?

It won't be tough for container provider to implement it. But container provider (entity manager implementor) need to follow what spec says to them.

I think it's easy to implement, but is this a problem in JPA spec or there is a merit behind these awkward to use API?
If this is a problem in the spec, is there a plan to improve in the next version?

Deepika Joshi wrote:
I think it doesn't make sense that if I want to refresh, or remove I need to merge first, and merging is updating, right? Moreover, I think it's very confusing that to refresh or remove I need to call merge *to update the entity first*( before I can refresh (reset) or remove it.
There is no point to merge (update) if I want to refresh or remove an entity.

I had same doubt in this thread, and I was answer by Ralph Jaus that requirement is entity should be attached before refresh/ remove is called and that is possible by calling find() before refresh/ remove is called
em.refresh(em.find(Item.class, item.id));

Thanks, but I am still not satisfied, because I think it should be like em.fresh(item), em.remove(item)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why EntityManager.refresh, and EntityManager.remove require attached entity?
 
Similar Threads
EntityManager
Hibernate: merge more than once causes OptimisticLockException
JPA Refresh Query from EJB3 In Action
Entitymanager.refresh doubt
EJB 3 IN ACTION - EntityManager merge() and refresh() method conflict