Search code examples
jpamappedsuperclass

JPA @MappedSuperclass No identifier specified for entity


I'm using Spring-Boot 1.5.10 with spring-boot-starter-data-jpa. I have a staging table and a production table, both have the same structure just different table names. Columns are:

  • compKey1
  • compKey2
  • compKey3
  • col_A
  • col_B
  • col_c

I am getting following error:

Caused by: org.hibernate.AnnotationException: No identifier specified for entity: com.foo.bar.StagingTbl

I have a composite primary key, class defined as:

@Embeddable
public class MyId implements Serializable {

        private static final long serialVersionUID = -99999999L;

        protected String compKey1;
        protected String compKey2;
        protected String compKey3;

       // setters/getters
}

my Abstract class:

        @MappedSuperclass
        public abstract class MyAbstractClass implements Serializable {

            protected static final long serialVersionUID = 7749572933971565230L;
            protected MyId myId;
            protected int col_A;
            protected Date col_B;
            protected String col_C

            public MyAbstractClass (String compKey1, String compKey2, String compKey3) {
              super();
              this.myId = new MyId(compKey1, compKey2, compKey3);
           }

            @EmbeddedId
            public MyId myId() {
                return myId;
            }
            public void setMyId(MyId myId) {
                this.myId= myId;
            }
          // getters/setters for other properties
}

my concrete class:

@Entity
@Table(name = "STG_TABLE" , schema="MYSCEMA")
public class StagingTbl extends MyAbstractClass implements Serializable {

}

The identifier should be coming from MyAbstractClass that I'm extending. I'm obviously missing something silly. Thanks in advance.


Solution

  • Your error is trivial: Hibernate only finds the @EmbeddedId on a method if it's name follows the Java Beans convention.

    Just change your getter to:

    @EmbeddedId
    public MyId getMyId() {
        return myId;
    }