Been going round in circles with this today, and wondering if I can ask for some help?
Unfortunately the actual code is company sensitive, but I've given what I believe to be a very similar abstraction representation...
2 Tables in the Database, namely Owner (id, name) and Pet (ownername, petname, age). The tables are linked by ownername and name for their respective tables. (This isn't ideal and the database is being normalised soon, but unfortunately this functionality needs to go in place first).
I retrieve all owners, and then loop through these get the Pet Collection. The entity mapping generated a PetsPK class which I used. Code follows:
I've got access to the database and for an owner I can see they've only got 3 associated pets. However this code returns 4.
By implementing a loop and checking for a petname equal to erronous entry, I remove it, and then persist the owner. Running code comes back with a size of 3 associated pets. However when I next deploy the application, the code returns to giving back a size of 4.
I've restarted the application domain.
The persist method in the bean looks like:
If I can provide any further information, please let me know...
PS. One thing worth mentioning is that earlier today I was attempting to rename the pet in a certain circumstance - say pet name was 'Bruno' I wanted to rename to 'Tyson'. The erronous record coming back is 'Tyson' which I can confirm is not in the database (I performed a SQL delete to remove that record from the db). So in otherwords I'm getting both 'Bruno' and 'Tyson' pet records come back.
this may be obvious have but you shut everything down and restarted to make sure the entity manager is "clean" (yes i have worked on a helpdesk)
as the problem may be entityManager getting confused by you changing the database behind it's back
Pretty new to jpa but trying to be helpful
Joined: Jun 30, 2010
Thanks for the reply. At home for the weekend now, so I'll request a server restart on Monday and see if that helps resolve the issue!
Joined: Jun 30, 2010
Taking this thread on a step. I've spent the evening regenerating the example at home using MySQL as the back end database. What I'm attempting to implement is:
If I start this program with 2 pet entries linked to Greg, and then execute this code, I end up with 4 entries - the 2 original for Greg, and 2 new ones for Joe.
What I'm attempting to do is replace the 2 original entities... am I missing a simple trick somewhere?
More than happy to post my entity classes from this example if you think they'll be of benefit?
[Edit] - As is alway the case after posting, I've found a 'solution'. Before updating the record, I save it away to another Collection. I then iterate through this new collection to remove these items from the original collection. This removes the 'old' entries. Is this the right way to do things though? Seems very long handed? Code follows:
The problem is that you are changing the Id/primary key of your object. You cannot do this, that is the point of an Id/primary key. I am surprised your database did not throw a constraint error, it must not have a primary key constraint defined.
Either do not define the name as the Id (perhaps generate a unique Id), or do not change the name (instead create a new Owner and set his name to the new name, and delete the old owner).
The reason you did not see the database change was TopLink maintains a cache by default. You could call refresh() on the object to refresh it from the databse, or disable the cache if desired.