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

stackUser2000 picture stackUser2000 · Sep 15, 2014 · Viewed 89.6k times · Source

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;
}

Answer

Cory Roy picture Cory Roy · Oct 14, 2015

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;