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; }
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.
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With