Tom
Tom

Reputation: 2444

How to persist a collection of interface's Collection<Interface> in JPA?

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

Upvotes: 1

Views: 1330

Answers (2)

Volksman
Volksman

Reputation: 2058

JDO does support it

Yes JDO does support persistent interfaces and we've been using them since 2007 in all of our designs because, you know, using interfaces in Java programming is like object oriented modeling 1.0.1. If your ORM doesn't support them then your so called 'transparent persistence' solution is actually not very transparent.

This and some other short comings have meant we have steered clear of the most popular JPA implementation and ended up using an ORM something slightly less popular but much more powerful and highly productive when it comes to object oriented modeling. We use DataNucleus/JDO where persistent interfaces work perfectly. I can't imagine building OO models without this support.

I'm not sure what the inherent architectural limitation is with the most popular JPA implementation that it can't support persistent interfaces.

As well as implementing the JDO standard DataNucleus also implements the JPA standard. There is a chance that DataNucleus/JPA does support persistent interfaces but I've only ever used DataNucleus with JDO so I don't know for sure.

but I am having difficulties getting JDO to place nicely with my Wicket app.

We have a massive (400+ persistent classes) web application/cloud platform deployed using JDO with the (most excellent) Wicket java UI framework and have never had a problem. We have created a couple of JDO specific IModel implementations that work with Wicket's model binding architecture. Let us know if you want to use these and we can open source them.

Upvotes: 0

Pascal Thivent
Pascal Thivent

Reputation: 570365

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.

Upvotes: 2

Related Questions