Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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; } 
like image 282
stackUser2000 Avatar asked Sep 15 '14 14:09

stackUser2000


People also ask

How do you fix no identifier specified for entity?

The error here is that in your Entity class, you have not defined a primary key. Thus specify either @Id annotation or an @EmbeddedId annotation. So the solution is just add @Id to appropriate primary key column. Thus every class defined as Entity with @Entity annotation, needs an @Id or @EmbeddedId property.

Is ID mandatory in hibernate?

Yes, hibernate requires an Id. Sometimes if you are dealing with a legacy database that for whatever reason does not have a key, you can define the key in Hibernate to be a composite key of all the columns for example, as this will be guaranteed to be unique.


1 Answers

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; 
like image 185
Cory Roy Avatar answered Sep 29 '22 09:09

Cory Roy