• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Curious about remove method

 
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Any sufficiently advanced technology will be used as a cat toy. And this tiny ad contains a very small cat:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic