File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Update OneToMany relation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Update OneToMany relation" Watch "Update OneToMany relation" New topic
Author

Update OneToMany relation

Enrico Morelli
Ranch Hand

Joined: Jan 07, 2013
Posts: 30
Dear all,

I'm new to Java EE and I have a problem to add relation to an entity.
I've the following entities:

Printers.java


Users.java


All works fine except when I try to relate a printer to an user. The first time the relation is written in the db. If I try to add another printer to the same user, I loose the first relation. I'm not able to add more than one printer to the same user. I tried a lot of solutions without result, the following is the last attempt to solve the problem.



Part of UsersEJB.java


Part of PrintersEJB.java


How can I solve the problem? Someone know book/sites where I can find real examples to use all the relations?

Thanks
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
A few things,

Users and Printers make no sense as class names, they should be singular, User, Printer.

In your User the printer @OneToMany should use a mappedBy="owner" not a @JoinColumn.

Nowhere in your savePrinter code do you add the printer to the user, you need to do this.

TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
Enrico Morelli
Ranch Hand

Joined: Jan 07, 2013
Posts: 30
Thanks,

in the mean time I find the solution (like you suggested) to add

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Update OneToMany relation