• 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

how to persist an object of a subclass from an existing super class entity object?

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello, i have a user entity which inherits from collaborator entity, and i use @Inheritance(strategy = InheritanceType.JOINED) in the Collaborator entity.
I do not find the way to persist a user entity that belongs to an existing collaborator,
i can find a collaborator by em.findById(2);
but how to associate a new user object to this collaborator object ?
 
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you User "has a" Collaborator, then you simply set it.

i.e.
user.setCollaborator(em.find(Collaborator.class, 2));
em.persist(user);

But you seem to be confused about inheritance wanting to make a new User "become" a Collaborator? If so, then you should not be using inheritance, but have User have a OneToOne relationship to Collaborator.
 
Jaaouane Aymen
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello, thak you for your help,
but i am not confused in inheritance,
User "is a" Collaborator, so i can not simply set it.
so, how i will set it in this situation?
I do not find the way to persist a user entity that belongs to an existing collaborator, I can add a user object and it results to 2 new created rows in the data base but what i want to do is to add only a user object and not a new collaborator.
I can find a collaborator by Collaborator clbr=em.findById(2);
and i want to add a user that its collaborator is clbr but how to associate the new user object to this collaborator object ?
Below my entities


Must I change my strategy to table per class?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic