Search code examples
javahibernatejpamappedsuperclass

Can I use @MappedSuperclass annotation on an interface?


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?

Solution

  • No. As stated here:

    JPA has no direct support for interfaces or variable relationships.