从@EmbeddedId类中提取超类时,实体没有持久的id属性

时间:2015-12-16 10:54:00

标签: java spring hibernate jpa

我在Spring Data JPA中使用Spring Boot 1.3。我想使用主键的专用对象来生成早期主键(如Implementing Domain Driven Design中所述)。

假设这个实体:

@Entity
public class Book {
  @EmbeddedId
  private BookId id;
}

和这个值对象:

@Embeddable
public class BookId implements Serializable {

  private UUID id;

  protected BookId(){} //for hibernate

  public BookId( UUID id ) {
    this.id = id;
  }

  public UUID getId() {
    return id;
  }
}

然后这很好用。但是,我想为所有id类创建一个超类,如:

public class EntityUuidId implements Serializable {

  private UUID id;

  protected EntityUuidId(){} //for hibernate

  public EntityUuidId( UUID id ) {
    this.id = id;
  }

  public UUID getId() {
    return id;
  }
}

现在BookId类更改为:

@Embeddable
public class BookId extends EntityUuidId {

  protected BookId(){} //for hibernate

  public BookId( UUID id ) {
    super(id);
  }
}

问题是,当我运行我的应用程序时,存在以下异常:

org.hibernate.AnnotationException: BookId has no persistent id property: Book.id

为什么突然间不再起作用?

1 个答案:

答案 0 :(得分:6)

@MappedSuperclass放在EntityUuidId类上,这样它的属性将被视为持久性。