Suppose you have a class like the following:
public class Container {
Element topElement;
Element rightElement;
Element leftElement;
.....
A possible DB mapping is done with a table Containers and another table Elements, linked by a foreign key like Container_ID. The table Elements will have a composite primary key made with Container_ID, the unique identifier of a Container object in the DB, and a flag indicating the element position (top, left, right, etc).
I use EclipseLink as ORM persistence provider, but I’m not an expert, so I usually start from the database design and let Netbeans build my entities from database with the wizard.
That way inside the Container entity I would get a Collection of Element, while I would like to have different fields (topElement, rightElement, etc) of the same type (Element). Is there a way to achieve that goal ?
Thanks
Filippo
You have a few options,
Just have the Collection variable, but provide get/set methods in your class that return the appropriate Element.
(probably the best solution).
Provide a get/set method for setting the Collection and internally set the fields.
Use 4 separate OneToOne relationships instead of a OneToMany. You could customizer the mapping to query the type, or change the data-model to match your object model.