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

101,219

Solution 1

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

Make sure it's:

import javax.persistence.Id;

and not:

import org.springframework.data.annotation.Id;

Solution 2

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.

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

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

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

Solution 3

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.

Solution 4

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

Solution 5

Its not juts Hibernate - a relational datamodel require 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.

for more info, here

Share:
101,219
stackUser2000
Author by

stackUser2000

Updated on July 09, 2022

Comments

  • stackUser2000
    stackUser2000 almost 2 years

    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?

    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;
    }
    
  • shanehoban
    shanehoban over 8 years
    would this require an actual column called 'id'?
  • ThMBc
    ThMBc over 8 years
    This solution requires it, but if you have a column in which you only have unique values, that could be the @Id column as well. If you decide to auto-generate one, you also need that column in your database.
  • shanehoban
    shanehoban over 8 years
    OK, what about where no unique value columns are being returned from a stored procedure? Could you perhaps try the following: @Id \n @GeneratedValue \n private int id;
  • ThMBc
    ThMBc over 8 years
    If you want to use hibernate and thus SQL you need to define a unique value column. The code provided automatically creates this column and fills it up. there are alternative storage systems where no primary keys are required but that is not in the scope of this question or answer. If you do not supply the @Column annotation it will create it for you.
  • sMaN
    sMaN about 8 years
    @Id needs to be an integer though, or convertible to an Integer, or am I mistaken?
  • sMaN
    sMaN about 8 years
    I am mistaken, my issue was elsewhere. I've managed to use an @Enumerated(EnumType.STRING) column as the id. I'm returning a single row of a single column, so this enum is unique by default.
  • Mike Argyriou
    Mike Argyriou almost 7 years
    "each row in a sql database should have a unique identifier" - this is not true.
  • amith
    amith over 6 years
    You really saved my day :)
  • Ibtissam Ibtissama
    Ibtissam Ibtissama about 5 years
    Thank you , you saved my day!!
  • FishingIsLife
    FishingIsLife over 4 years
    So simple but that happens when you rely on your ide :/ Thank you.
  • Klesun
    Klesun almost 3 years
    OP was specifically asking what to do when table has no primary key column to mark with @Id.
  • Admin
    Admin over 2 years
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.