1

I have a "festival" entity that is related to a "merchant" (many to one) and the merchant is related to "category" (many to one)

In one of my pages, I load all categories then get list of merchants for it and after the user chooses favorite merchant then I load merchants festivals list. finally when the user deletes favorite festival I remove it with : em.remove()

but the deleted festival is still existing in the Merchant's festival list and also category.merchantList(n).festival(m) !

so when i reload the page I find the deleted festival while its not in database anymore !

so how should I update the merchant and category entities?!

Karthikeyan Vaithilingam
  • 6,473
  • 10
  • 41
  • 60
Hamid Adldoost
  • 149
  • 1
  • 15

1 Answers1

0

With bidirectional entity relationships, you need to maintain both sides of the relationship. That means you need to remove the festival from the collections that contain it (merchant.festivals in your case).

This is an easy task when there are only two entities involved. In more complex models, it could be easier to refresh all relevant data from the database after you change / remove some entity. This is because em.remove() will fire delete statement into database, resulting in the deletion of the row, but it needs to be synchronized with entities already loaded in entity manager's cache.

Predrag Maric
  • 21,996
  • 4
  • 45
  • 64
  • how should I refresh all relevant data in entity manager? also i am using a generic dao that provides all CRUD functions for my all entities. – Hamid Adldoost Apr 06 '15 at 09:20
  • `em.refresh()` refreshes the entity from database. If `cascade = CascadeType.REFRESH` is included on relationship mapping, it will be propagated. – Predrag Maric Apr 06 '15 at 09:25
  • thanks for that. how about using cascade = CascadeType.REMOVE ? will it resolve my problem without using refresh? will it propagate to loaded entities too? – Hamid Adldoost Apr 06 '15 at 09:31
  • No, it means when you remove parent, child should be removed also. But if your real model is as simple as this example in your question, it shouldn't be a problem to just remove the deleted entity from the collection? – Predrag Maric Apr 06 '15 at 09:35
  • Actually I worry about other objects maybe existing in my application that includes a "festival" field too. how can I get sure of changing all objects that includes my deleted festival? – Hamid Adldoost Apr 06 '15 at 09:45
  • Do you have L2 cache configured in your application? – Predrag Maric Apr 06 '15 at 10:35
  • I don't think so. I am using JPA2.1 with eclipseLink and I have generated my entity classes with netbeans auto generation. does it do any default second level cache configuration?! – Hamid Adldoost Apr 06 '15 at 10:51
  • 1
    Without L2 there shouldn't be a problem. Simple query should return fresh data. Check [this post](http://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink) for more info. Do you have `` or `` in your `persistence.xml`? – Predrag Maric Apr 06 '15 at 11:27
  • Wow.. thank you very much!! I added that row in my persistence.xml and its now working like a charm :D will it make my application slower?! – Hamid Adldoost Apr 06 '15 at 11:45
  • 1
    Maybe, depending on number of queries and their complexity.But with database indexes in place and reasonable number of rows in database it should be ok. There will always be a compromise between performance and data "freshness", just find the balance that you are ok with. – Predrag Maric Apr 06 '15 at 12:01