我在Spring Data JPA中使用Spring Boot 1.3.我想使用主键的专用对象来生成早期主键(如实现域驱动设计中所建议的那样).
假设这个实体:
@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
为什么这突然不再起作用了?
把@MappedSuperclass
对EntityUuidId
类,这样,其属性将被视为永久.