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

Curious about remove method

 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As i know for method

entityManager.find(Car.class, car_id);

by specifying the primay key, i can retireve the record.

But how can remove the record by specifying the primary key.

like here

entityManager.remove(car);

I can only specify the entity type object. Is not possible to remove the object by specifying the primary key.
 
Chaminda Amarasinghe
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
find will work even outside of the active transaction, but not remove.

in other words only managed entities can be removed. that means to use remove method you need a managed entity not just id. so there is no remove(Id) method

you can implement your own remove method as follows

em.remove(em.find(clazz,id));
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
find will work even outside of the active transaction, but not remove.


i think this is incorrect, because find always work inside the existing transaction. So here i can say is that- when the entity is unmanged, doing find will make it managed entity.

in other words only managed entities can be removed. that means to use remove method you need a managed entity not just id. so there is no remove(Id) method

Correct.

you can implement your own remove method as follows

em.remove(em.find(clazz,id))


this is execellent, thanks Chaminda.
 
Chaminda Amarasinghe
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
quote: find will work even outside of the active transaction, but not remove.



i think this is incorrect, because find always work inside the existing transaction. So here i can say is that- when the entity is unmanged, doing find will make it managed entity.


Your thought totally incorrect according to the spec. See jpa spec 3.1.1 EntityManager Interface

The find and getReference methods are not required to be invoked within a transaction context. If
an entity manager with transaction-scoped persistence context is in use, the resulting entities will be
detached; if an entity manager with an extended persistence context is used, they will be managed. See
section 3.3 for entity manager use outside a transaction



According to the spec my explanation is correct.


in other words only managed entities can be removed. that means to use remove method you need a managed entity not just id. so there is no remove(Id) method


Correct.

quote:you can implement your own remove method as follows

em.remove(em.find(clazz,id))



this is execellent, thanks Chaminda.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic