我可以在接口上使用@MappedSuperclass注释吗? [英] Can I use @MappedSuperclass annotation on an interface?

查看:100
本文介绍了我可以在接口上使用@MappedSuperclass注释吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在interface中对通用映射进行分组,但是我不能使用abstract superclass,因为我的实体已经扩展了另一个class.因此,我需要一个如下所示的界面:

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. 我可以在接口上使用@MappedSuperclass注释吗?
  2. Hibernate是否支持接口?

推荐答案

否.如所述此处:

JPA不直接支持接口或变量关系.

JPA has no direct support for interfaces or variable relationships.

这篇关于我可以在接口上使用@MappedSuperclass注释吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆