permaculture playing cards*
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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
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 ?
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to persist an object of a subclass from an existing super class entity object?
 
Similar Threads
Problem with One-To-Many bi-directional mapping
PersistentObjectException: detached entity passed to persist
Required information on session.merge()
How do you update a collection if some exist and some are new?
find and getReference methods