Java Org.Hibernate.AnnotationException:没有为实体指定标识符 我的表中没有 id

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25851029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:16:51  来源:igfitidea点击:

Org.Hibernate.AnnotationException: No Identifier Specified For Entity I don't have a id in my table

javahibernatespring-mvcjpaspring-data

提问by stackUser2000

I'm working with a table in a database and that table don't have a primary key or a proper column whith a unique value who can act as a primary key, I don't have permissions to alter that table.

我正在处理数据库中的一个表,该表没有主键或具有唯一值的适当列可以充当主键,我无权更改该表。

What should I do? I tried putting a @id annotation in a random column and it worked, but I don't know if this is going to bring any trouble later on. what can I do?

我该怎么办?我尝试将 @id 注释放在一个随机列中并且它起作用了,但我不知道这是否会在以后带来任何麻烦。我能做什么?

My class

我的课

@Entity
@Table(name="my_table")
public class TheTable {
@Column (name="name", nullable=false)
    private String name;

    @Id <--- I just put this id in this random column but this column should not be a id column
    @Column (name="anyfield", nullable=false)
    private String anyfield;
}

回答by ThMBc

Hibernate is an intermediate to adress SQL databases, and since each row in a sql database should have a unique identifier, hibernate forces you to define one.

Hibernate 是寻址 SQL 数据库的中间版本,由于 sql 数据库中的每一行都应该有一个唯一标识符,因此 Hibernate 强制您定义一个。

Here is an example of a generated primary key, which will be added automatically (do not forget the getter and setter)

下面是一个生成主键的例子,它会自动添加(不要忘记getter和setter)

@Id 
@GeneratedValue
@Column(name = "id")
private int id;

Since duplicates are not allowed, choosing an arbitrary column is not the way to go.

由于不允许重复,因此选择任意列不是可行的方法。

回答by Ankur Singhal

Its not juts Hibernate- a relational datamodelrequire primary keys. So what you've got is a broken data model because without a primary key it can't be relational, and this is why its difficult to use with an ORM.

它不是突出Hibernate- 一个关系datamodel要求primary keys。所以你得到的是一个破碎的数据模型,因为没有主键它不能是关系的,这就是为什么它很难与 ORM 一起使用。

for more info, here

欲了解更多信息,请点击此处

回答by Ankur Singhal

JPA (not Hibernate as such) requires all entities to be uniquely identified. Sadly it doesn't allow for what you want.

JPA(不是 Hibernate 本身)要求唯一标识所有实体。可悲的是,它不允许你想要什么。

JDO, on the other hand, does allow a persistable class to map to a table without PK and would handle what you need.

另一方面,JDO 确实允许持久类映射到没有 PK 的表,并且可以处理您需要的内容。

回答by Manisha Srivastava

You can solve this using embedded id 

ex:

前任:

@Entity
@Table(name = "my_table")
public class MyTable implements Serializable {

    private static final long serialVersionUID = 1L;

    // @Id
    @Column(name = "id", insertable = false, updatable = false)
    private String id;

    @EmbeddedId
    MYtablePK pk;

    public MYtablePK getPk() {
        return pk;
    }

    public void setPk(MYtablePK pk) {
        this.pk = pk;
    }

    @Column(name = "my_table_FirstName", insertable = false, updatable = false)
    private String name;

    @Transient
    public String getName() {
        return pk.getName();
    }

    public void setName(String mrn) {
        pk.setName(name);
    }
    @Transient
        public String getSeverity() {
        return pk.getSeverity();
    }
    public void setSeverity(String severity) {
        pk.setName(name);
    }

    public String getId() {
        return pk.getId();
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return pk.getName();
    }
    public void setName(String name) {
        tpk.setName(name);
    }
}

@Embeddable
public class MyTablePK implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = -1257821517891392898L;
    @Column(name = "id")
    private String id;
    @Column(name = "name")
    private String name;
    @Column(name = "dob")
    private Date dob;
    public Date getdob() {
        return dob;
    }
    public void setdob(Date dob) {
        this.pk.setdob(dob);
    }
    @Column(name = "severity")
    private String severity;
    public String getSeverity() {
        return severity;
    }
    public void setSeverity(String severity) {
        this.severity = severity;
    }

    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

回答by Cory Roy

I had this problem and was using the wrong import for @id:

我遇到了这个问题,并且对@id 使用了错误的导入:

Make sure it's:

确保它是:

import javax.persistence.Id;

and not:

并不是:

import org.springframework.data.annotation.Id;

回答by Shivalik Chopra

Make sure there is p.k constraint defined for each Entity and check the import statement for Id Annotation. Check Import statement:

确保为每个实体定义了 pk 约束,并检查 Id 注释的导入语句。检查导入语句:

import javax.persistence.Id;
@Id
@Column(name="id")
@GeneratedValue 
private int id;

回答by user2389736

Also had same issue. The problem was wrong import of @Id annotation. So please make sure that you not only annotate the id but also that you do it with javax.persistence.Id.

也有同样的问题。问题是 @Id 注释的错误导入。因此,请确保您不仅注释 id,而且还使用 javax.persistence.Id 进行注释。

回答by user7420004

Specify a field annotated with @Id. Every @Entity requires an @Id (This is the primary key in the table). The solution for you is to use @Embeddable instead of @Entity, then you wont need to annotate any column with @Id. Change the import statement from javax.persistence.Entity; to javax.persistence.Embeddable;

指定一个用@Id 注释的字段。每个@Entity 都需要一个@Id(这是表中的主键)。您的解决方案是使用@Embeddable 而不是@Entity,那么您就不需要用@Id 注释任何列。从 javax.persistence.Entity 更改 import 语句;到 javax.persistence.Embeddable;

回答by GovindaRaju

Use @Embeddableannotation instead of @Entityif your class mapping is used as readOnlymapping and does not have PKor @EmbeddedKey

使用@Embeddable替代的注解@Entity,如果你的类映射作为readOnly映射,没有PK@EmbeddedKey

//@Entity
@Embeddable
@Table(name = "my_table")
public class MyTable implements Serializable {

}