5

I want to group common mappings in an interface, but I cannot use an abstract superclass because my entities already extend another class. So I need an interface like below:

@MappedSuperclass
public interface NamedEntity {
    @Column(name = "name")
    String getName();
    void setName(String name);
}

and I want to use it like below:

public class Person {
    private Long id;
    private String name;
    public Long getId(){ return id; }
    public void setId(Long id){ this.id = id; }
    public String getName() { return name; }
    public void setName(String name){ this.name = name; }
}

@Entity
@Table(name = "person_entity")
public class PersonEntity extends Person implements NamedEntity {
    @Id
    @GeneratedValue
    @Column(name = "id")
    @Override
    public Long getId() { return super.getId() }
}

Would this work, I mean;

  1. Can I use @MappedSuperclass annotation on an interface?
  2. Does Hibernate have support for interfaces?
Bahattin Ungormus
  • 628
  • 1
  • 9
  • 23

1 Answers1

7

No. As stated here:

JPA has no direct support for interfaces or variable relationships.

Imus
  • 802
  • 6
  • 11
  • Thank you. Another question: Does Hibernate have support for interfaces? – Bahattin Ungormus Mar 01 '18 at 08:12
  • 1
    I'm not too familiar myself with ibernate. Perhaps [this post](https://stackoverflow.com/questions/2912988/persist-collection-of-interface-using-hibernate) tells you what you need to know? – Imus Mar 01 '18 at 08:22
  • small addition .... In JPA your target is to save something (i.e. entities) ... thus this thing to save should be persistable (i.e. has state) , mapped super class is just a common part of your entity that can be shared among other entities so the same rule applies .... interface is not something to hold a 'state' , it's rather a definition for behavior – osama yaccoub Mar 01 '18 at 08:33