Consider the following two entities:
Now, DAO for these classes:
And the problematic scenario:
Right now we have a shop and two books assigned to it in the database.
Now I want to replace an old book with new one:
After that I expect to have a bookShop in database as well as books: "Planets" and "Galaxies", and book "Stars" to be deleted.
The problem is that instead I have all three books in database: "Planets", "Stars" and "Galaxies".
My questions:
- Is there any annotation which detects such changes and makes them in database as well?
- ...or should I change my approach by preventing the use of new operator in such cases?
- Are there any good solutions (annotations or just
patterns) for handling the removing and adding books to the shop in a way that will also update the database properly?