The moose likes Object Relational Mapping and the fly likes how to persist an object of a subclass from an existing super class entity object? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Object Relational Mapping
Reply Bookmark "how to persist an object of a subclass from an existing super class entity object?" Watch "how to persist an object of a subclass from an existing super class entity object?" New topic
Author

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

Jaaouane Aymen
Greenhorn

Joined: Sep 22, 2009
Posts: 29
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 ?

This message was edited 1 time. Last update was at by Jaaouane Aymen

James Sutherland
Ranch Hand

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


TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
Jaaouane Aymen
Greenhorn

Joined: Sep 22, 2009
Posts: 29
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?
 
 
subject: how to persist an object of a subclass from an existing super class entity object?
 
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture