• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Update OneToMany relation

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Enrico Morelli
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,

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

 
That's a very big dog. I think I want to go home now and hug this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic