ORM of 1-m relationships between interfaces

Discussions

General J2EE: ORM of 1-m relationships between interfaces

  1. ORM of 1-m relationships between interfaces (1 messages)

    Hi there! I would like to map a 1-m relationship from one interface to another without exposing the collection on the owning side of the relationship. Say I've got the two interfaces Order and OrderItem: public interface Order { void addItem(OrderItem item); void removeItem(OrderItem item); Iterator itemIterator(); } public interface OrderItem { Order getOrder(); int getQuantity(); Product getProduct(); } Is there any ORM tool that can map this? So far, I've looked at JPA (JBoss EJB3), Hibernate and JDO (JPOX), but none of them seems to be able to do this. While JPA doesn't support interfaces at all (interfaces are not supposed to be annotated for persistence), Hibernate and JDO both force me to expose the collection by implementing Order.setOrderItems(Collection items) and Collection Order.getOrderItems(), which is not what I want. Another important requirement is that the relationship between the two interfaces must support polymorphism, i.e. if there are multiple (mapped) implementations of each interface the iterator returned by Order.itemIterator() and OrderItem.getOrder() should return instances of the correct concrete class. If anyone can think of a way to implement this mapping, I would be most grateful to hear about it. Many thanks Kai
  2. You need to separate the concepts of interfaces, and implementation classes. Your order interface is perfectly ok, but you implementation class (say OrderImpl) will need to expose all of those methods that (for example) Hibernate needs to solve the orm issues. However, you only expose the interface to any calling classes/components, thus hiding the "dirty" methods. That way, at least Hibernate May solve the problem. A rough example: public interface Order { void addItem(OrderItem item); void removeItem(OrderItem item); Iterator itemIterator(); } public class OrderImpl implements Order { Set items = new HashSet(); public void addItem(OrderItem item) {...} public void removeItem(OrderItem item) {...} public (or even private will do!) void setItems(Set items) {...} public (or even private will do!) Set getItems() {...} Iterator itemIterator() {...} } Hibernate will work with OrderImpl, but the calling code always uses Order. /Niklas