aspose file tools*
The moose likes Object Relational Mapping and the fly likes Additional Entries in EJB over database (Oracle TopLink) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Additional Entries in EJB over database (Oracle TopLink)" Watch "Additional Entries in EJB over database (Oracle TopLink)" New topic
Author

Additional Entries in EJB over database (Oracle TopLink)

Greg Bowie
Greenhorn

Joined: Jun 30, 2010
Posts: 6
Hi all,

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.
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1106

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

Wendy
Pretty new to jpa but trying to be helpful
Greg Bowie
Greenhorn

Joined: Jun 30, 2010
Posts: 6
Hi Wendy,

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!

Cheers,
Greg
Greg Bowie
Greenhorn

Joined: Jun 30, 2010
Posts: 6
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?

Cheers,
Greg

[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:

Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1106

i am not sure what owner.persist does but does it always persist, or does it sometimes update?
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
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.


TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Additional Entries in EJB over database (Oracle TopLink)
 
Similar Threads
Unable to access pet list, Hibernate problem ?
Displaying things...need help badly!! Please!
Digester help
Input/Output PetRecord
Abstract class with abstract class passed to constructor