在实体中嵌入复杂对象

Embed complex object in entity

我要嵌入以下内容

 @Embeddable
  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 {
    
      @EmbeddedId
      private BaseEntity baseEntity;
    
    
      @Column(length = 80, nullable = false)
      private String name;
}

所以我生成的 table 看起来像这样

room
  id
  createdDate
  name

但是 id 和 createdDate 没有被嵌入

而不是 @Embeddable 只需扩展您的 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;

}