Can I use @MappedSuperclass annotation on an interface?

泄露秘密 提交于 2019-12-01 23:42:10

问题


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?

回答1:


No. As stated here:

JPA has no direct support for interfaces or variable relationships.



来源:https://stackoverflow.com/questions/49045027/can-i-use-mappedsuperclass-annotation-on-an-interface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!