views:

43

answers:

1

Hi, I have an the following scenario:

// several classes that implement different interfaces 
class A implements X,Y {}
class B implements Y,Z {}
class C implements X,Z {}

// other classes that contain collections of one of the interfaces(different objects)
class G {
  Collection<X> mayContainAC;
}
class H {
  Collection<Y> mayContainAB;
}
class I {
  Collection<Z> mayContainBC;
}

How would I go about persisting this using JPA?

From what I can see JPA doesn't support Collections of interfaces. Is the correct? JDO does support it but I am having difficulties getting JDO to place nicely with my Wicket app.

Thanks, Tom

+2  A: 

How would I go about persisting this using JPA?

Not supported.

From what I can see JPA doesn't support Collections of interfaces. Is the correct?

If the interface has a single persistent implementer, then you can define it using the targetEntity.

If the interface has multiple implementers, it's not supported by standard JPA.

Pascal Thivent
thanks for confirmation - whats the recommended workaround for the scenario I have described?
Tom
@Tom You could maybe persist separated collections and merge them yourself.
Pascal Thivent
Tom
Pascal, congratulations on passing the 100K reputation mark. Most impressive.
duffymo
@duffymo Thank you very much duffy.
Pascal Thivent
@Tom A common abstract class is the traditional approach. I'm not *sure* Scala or anything else could help.
Pascal Thivent
I guess one way would be to have abstract base classes called, ThingsThatGoInG, ThingsThatGoInH and ThingsThatGoInI and then some container classes; AinG class that has-a A as a property, BinG class that has-a B as a property, BinH that has-a B property etc. Lots of extra classes but allows for flexible grouping and follows the open/closed priniple better. You would have one level of indirection to reach the actual class and it would have more complicated JOINs but wouldnt rely on UNIONs.
Tom
@Tom The inheritance + composition approach that you are describing should do it.
Pascal Thivent