Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity has no persistent id property when extracting a superclass from @EmbeddedId class

I am using Spring Boot 1.3 with Spring Data JPA. I have want to use early primary key generation using a dedicated object for the primary key (As advised in Implementing Domain Driven Design).

Suppose this entity:

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

and this value object:

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

Then this works fine. However, I want to create a superclass for all id classes, something like:

public class EntityUuidId implements Serializable {

  private UUID id;

  protected EntityUuidId(){} //for hibernate

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

  public UUID getId() {
    return id;
  }
}

Now the BookId class changes to:

@Embeddable
public class BookId extends EntityUuidId {

  protected BookId(){} //for hibernate

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

The problem is now when I run my application there is the following exception:

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

Why does that suddenly not work anymore?

like image 415
Wim Deblauwe Avatar asked Dec 16 '15 10:12

Wim Deblauwe


1 Answers

Put @MappedSuperclass on EntityUuidId class, that way its properties will be treated as persistent.

like image 118
Predrag Maric Avatar answered Nov 18 '22 00:11

Predrag Maric