spring在实体中嵌入复杂对象

lskq00tm  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(281)

我想嵌入以下内容

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

生成的表如下所示

room
  id
  createdDate
  name

但是id和createddate没有嵌入

wsewodh2

wsewodh21#

而不是 @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;

}

相关问题