springspring-bootjpaembeddable

Embed complex object in entity


I want to embed the following

 @Embeddable
  public class BaseEntity implements Serializable {

  @Id
  @GeneratedValue
  private UUID id;

  @CreatedDate
  @Column(name = "created_date", updatable = false)
  private LocalDateTime createdDate;

 }

into my room entity

    @Entity
    @Data
    @NoArgsConstructor
    @Table(name = "room")
    public class room {
    
      @EmbeddedId
      private BaseEntity baseEntity;
    
    
      @Column(length = 80, nullable = false)
      private String name;
}

So that my generated table looks like this

room
  id
  createdDate
  name

But id and createdDate are not getting embedded


Solution

  • Instead of @Embeddable just extend your BaseEntity

    @MappedSuperclass 
    @Getter
    @Setter
    public class BaseEntity implements Serializable {
    
      @Id
      @GeneratedValue
      private UUID id;
    
      @CreatedDate
      @Column(name = "created_date", updatable = false)
      private LocalDateTime createdDate;
    
     }
    
    @Entity
    @Data
    @NoArgsConstructor
    @Table(name = "room")
    public class room extends BaseEntity{
    
      @Column(length = 80, nullable = false)
      private String name;
    
    }