1

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?

Pazonec
  • 1,549
  • 1
  • 11
  • 35

2 Answers2

3

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

James
  • 17,965
  • 11
  • 91
  • 146
  • James, I'm trying to persist the Filter class that contains a set of FilterOtions(Set). Interface FilterOption has a lot of different implementations. I can't use abstract and still can't resolve this problem. I heard that it's possible to make it by hibernate xml mapping – Pazonec Jan 25 '12 at 16:45
2

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.

Community
  • 1
  • 1
NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
  • Thanks. But how can I make a ManyToOne relationship on List. FilterOption is the interfase wich has a lot of implementations. And I want to have the collection of this types stored in one table. Is it impossible? – Pazonec Jan 24 '12 at 16:50
  • I don't really understand what you mean. What data do you want to be persisted, annotate that class. – NimChimpsky Jan 24 '12 at 16:52