File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Hong Anderson
Ranch Hand
Posts: 1936
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
jim li
Ranch Hand
Posts: 180
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 268
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1936
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1936
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic