1

I have a table where there isn't a primary key. It is expecting an @ID annotation and without it, the following error is being thrown:

Caused by: org.hibernate.AnnotationException: No identifier specified for entity: 
overall.model.Overall
    at org.hibernate.cfg.InheritanceState.determineDefaultAccessType(InheritanceState.java:266) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
    at org.hibernate.cfg.InheritanceState.getElementsToProcess(InheritanceState.java:211) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:781) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]

I have tried implementing both in JDBC template as well as using JPA but I'm not able to create any entity without @ID annotation.

The table has all fields of type String. Could you please give a suggestion on how to configure a table to not have a primary key?

I am using a spring-boot.

Emma
  • 27,428
  • 11
  • 44
  • 69
  • [This](https://stackoverflow.com/questions/41143913/sql-jpa-multiple-columns-as-primary-key) should help. – Andrew S Jul 23 '20 at 12:19

1 Answers1

0

Since the table has no primary key at all, use this workaround: create a fake composite primary key, consisting of all the columns. I hope there aren't too many of them. For example:

@Entity
@IdClass(UserNRole.InnerIdClass.class)
@Table(name = "users_n_roles")
public class UserNRole {
    @Id
    @Column(name = "user_id")
    private Integer userId;

    @Id
    @Column(name = "role_id")
    private Integer roleId;
 
    // getters, setters and constructor . . .
 
    public static class InnerIdClass implements Serializable {
        private Integer userId;
        private Integer roleId;
    }
}