JPA mapping Interface as @Entity in EclipseLink

2019-07-25 13:38发布

问题:

I have to map the interface as entity to use different implementations in collections.

@Entity
@Table(name = "OPTION")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "TYPE", discriminatorType = DiscriminatorType.STRING)
public interface FilterOption {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId();
    public void setId(Long id);
    public Object getValue();
...
}

I'm recieving an error :

Internal Exception: Exception [EclipseLink-7161] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.ValidationException Exception Description: Entity class [class FilterOption] has no primary key specified. It should define either an @Id, @EmbeddedId or an @IdClass.

As you can see the @Id is declared. How can I fix this problem?

回答1:

you can't map entities to interfaces using annotations with eclipselink, apparently.

In fact, you can't map an entity to an interface at all using jpa or hibernate.



回答2:

You cannot map interfaces as Entity. What exactly are you trying to do?

Perhaps create an abstract class AbstractFilterOption and make it an Entity or MappedSuperclass.

EclipseLink does have interface support, but not currently through JPA annotations. You can define an interface descriptor for an interface, but it will not have a table, it will just query its implementations. You can also use the @VariableOneToOne annotation to map a variable relationship based on an interface.

See, http://en.wikibooks.org/wiki/Java_Persistence/Advanced_Topics#Interfaces