views:

256

answers:

2

I have a generic class that is also a mapped super class that has a private field that holds a pointer to another object of the same type:

@MappedSuperclass
public abstract class MyClass<T extends MyIfc<T>>
    implements MyIfc<T>
    {

        @OneToOne()
        @JoinColumn(name = "previous", nullable = true)
        private T previous;

             ...
             }

My problem is that Eclipse is showing an error in the file at the OneToOne "Target Entity "T" for previous is not an Entity." All of the implementations of MyIfc are, in fact, Entities. I should also add that each concrete implementation that inherit from MyClass uses a different value for T (because T is itself) so I can't use the "targetEntity" attribute.

I guess if there is no answer then I'll have to move this JPA annotation to all the concrete subclasses of MyClass. It just seems like JPA/Hibernate should be smart enough to know it'll all work out at run-time. Makes me wonder if I should just ignore this error somehow.

+2  A: 

My problem is that Eclipse is showing an error in the file at the OneToOne "Target Entity "T" for previous is not an Entity."

Yes, and even if T was extending an Entity, I am not aware of any JPA provider supporting this (that's just not part of the JPA spec anyway). For more feedback have a look at JPA Generic entities classes Mappedsuperclass are not possible! (very similar thread about EclipseLink):

No you will be unable to make the Entities generic. The provider will be unable to map the relationship to the specific type defined by the generic definition as this type is assigned when the Entity is created in code not where the Entity is defined. Remember when designating Generics the Collection (in this case) is limited only to those types. The Provider can not possibly be this restrictive on a per Entity instance basis. In some cases changing the type may result in entirely different tables being mapped for a single Entity instance and that is definitely not supported.

Pascal Thivent
Thanks for confirming that this is impossible. Yet another reason why implementation inheritance never works out the way you want. I'm going to remove all references to generic types off the abstract superclass and duplicate them in all the concrete implementations.I wonder if I would have been better off using XML mapping files instead of annotations?
HDave
@HDave I don't *think* that this would change anything, I'd expect your JPA provider to complain at runtime.
Pascal Thivent
Even if it didn't I suppose compile-type type-safe entity relationships are worth the peace of mind. It just stinks that I have to spend today copying this snippet of code to 17 concrete entity implementations!
HDave
+2  A: 

Since JDO supports persistence of interface fields (which is a similar concept to what you have here), and since DataNucleus JPA is built on top of the JDO capabilities, then it likely would allow you to persist such a field (I have an example using JDO that does something very similar, but without seeing the remains of your classes and persistence code its impossible to be definitive). Give it a try and see what happens.

Obviously that is beyond the JPA spec, hence if portability is a concern for you then have a think first

DataNucleus
In fact I've also tried it with JPA and that also works. So no, it is not impossible, as implied on the other response. It is beyond the JPA spec certainly, but very possible. You just need to use DataNucleus
DataNucleus