How to detect polymorphism in JPA

there is a situation. For example, I am developing a simple blog. There are articles and photos. Users can add a comment to both. So when I write it in Java, it looks like this:

public interface Commentable { ... }

public class Article implements Commentable { ... }

public class Photo implements Commentable { ... }

public class Comment { 
   ...
   private Commentable commentTo; 
}

This is clear, and I hope the design is correct. But now I would like to save it to the database, and I want to use JPA annotations. First of all, I have to use JPA 1.0, but if there is no solution, I would like to know how to do it in JPA 2.0. I found out that there is a way with classic inheritance, but I think Commentable should not be the parent of this object, it is only an extension in the light of design.

Is there a way to save it without changing the design, please? Many thanks

+5
1

, ?

JPA (, , , , ). , JPA (, EclipseLink @VariableOneToOne).

JPA ( ) . ( ):

public class Comment { 
   ...
   @OneToOne(targetEntity=MyAbstractBaseClass.class)
   private Commentable commentTo; 
}

Article Photo MyAbstractBaseClass .

JPA wikibook , . .

  • JPA Wikibook:
+8

All Articles