如何在superClass中使用@Id通过在Hibernate中的另一个类中使用@Embedable

时间:2017-07-21 07:36:28

标签: hibernate

我有2个类,一个是Student.class(Embedded),另一个是CommonDetails.class(Embeddable)。我想创建一个学生表,其中包含id,name,email和id字段作为标识符。

但它抛出了这个例外:

No identifier specified for entity: com.icore.mint.data.customer.service.usage.Student.

是否有任何我不想在id中创建字段Student.class的解决方案。我想使用id

中的CommonDetails.class.

以下是代码。

CommonDetails.class

@Embeddable
public class CommonDetails implements Serializable {
    private static final long serialVersionUID = 1L;
    String id;
    String name;
    String email;
    //Setters and Getters
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    //toString()
    @Override
    public String toString() {
        return "CommonDetails [id=" + id + ", name=" + name + ",
         email=" + email + "]";
    }
}//Class

Student.class

@Table
@Table(name="student")
public class Student implements Serializable{
    private static final long serialVersionUID = 1L;
    CommonDetails commonDetails;
    //Setters and Getters
    @Embedded
    @AttributeOverrides({
    @AttributeOverride(name = "id", column = @Column(name = "id")),
    @AttributeOverride(name = "name", column = @Column(name = 
    "name")),
    @AttributeOverride(name = "email", column = @Column(name = 
    "email"))
    })
    public CommonDetails getCommonDetails() {
        return commonDetails;
    }
    public void setCommonDetails(CommonDetails commonDetails) {
        this.commonDetails = commonDetails;
    }
    //toString()
    @Override
    public String toString() {
        return "Student [commonDetails=" + commonDetails + "]";
    }
}//Class

1 个答案:

答案 0 :(得分:1)

我认为您正在搜索 @EmbeddedId 注释。

这是我项目的例子:

@Embeddable注释的类:

@Embeddable 
public class SetLogs implements Serializable{

    private static final long serialVersionUID = 1L;
    @Column(name="user_id")
    private Integer user_id;

    @Column(name="menu_id")
    private Integer menu_id; 

    @Column(name="logs_time")
    private Date logs_time;

}

@EmbeddedId注释的类:

@Entity
@Table(name="logs")
public class Logs implements Serializable{

    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private SetLogs setLogs;

    public SetLogs getSetLogs() {
        return setLogs;
    }

    public void setSetLogs(SetLogs setLogs) {
        this.setLogs = setLogs;
    }
}

您也可以参考此链接http://www.concretepage.com/hibernate/example-embeddedid-hibernate

希望这会对你有所帮助。