【问题标题】:JPA @MappedSuperclass No identifier specified for entityJPA @MappedSuperclass 没有为实体指定标识符
【发布时间】:2018-08-13 21:41:42
【问题描述】:

我正在使用带有 spring-boot-starter-data-jpa 的 Spring-Boot 1.5.10。我有一个临时表和一个生产表,它们的结构相同,只是表名不同。列是:

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

我收到以下错误:

原因:org.hibernate.AnnotationException:未指定标识符 对于实体:com.foo.bar.StagingTbl

我有一个复合主键,类定义为:

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

我的具体课程:

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

}

标识符应该来自我正在扩展的 MyAbstractClass。我显然错过了一些愚蠢的事情。提前致谢。

【问题讨论】:

    标签: jpa mappedsuperclass


    【解决方案1】:

    您的错误是微不足道的:Hibernate 仅在名称遵循 Java Beans 约定的方法上找到 @EmbeddedId。

    只需将您的吸气剂更改为:

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

    【讨论】:

    • omg ...这让我发疯了。感谢您发现...我让 ide 自动生成 getter/setter,但我没有注意到
    • 几年来我第一次看到这个错误和解决方案。谢谢。
    猜你喜欢
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    • 2015-07-25
    • 2016-01-31
    • 1970-01-01
    • 1970-01-01
    • 2020-05-02
    相关资源
    最近更新 更多