You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于现有类生成@Entity实体类?

实现领域模型与持久化层的解耦

我有一个规模较大的对象模型,需要将模型定义与持久化层解耦。下面是我用来生成实体类的示例领域模型类:

@Setter
@Getter
public abstract class AbstractAccount {
  protected Long id;
  protected AbstractProfile profile;
  protected AbstractAuthorization<AbstractRole> authorization;
  protected AbstractWallet wallet;
  protected AbstractActivityHistory<AbstractGameSessionRecord, AbstractOperationRecord> history;

  // 构造方法及其他代码...
}

虽然直接给每个类标记@Entity并修改字段注解就能实现持久化,但我需要严格实现模型与持久化层的解耦,期望生成如下结构的实体类:

@Data
@Entity
@SuperBuilder
@Inheritance(strategy = InheritanceType.JOINED)
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public abstract class AccountEntity {
  public AccountEntity(AccountType accountType) {
    this.accountType = accountType;
  }

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  public Long id;

  @Embedded
  public AbstractWalletEntity walletEntity;

  @Embedded
  public AbstractProfileEntity profileEntity;

  @Embedded
  AbstractAuthorizationEntity authorizationEntity;

  @Column(nullable = false)
  @Enumerated(EnumType.STRING)
  public AccountType accountType;
}

内容的提问来源于stack exchange,提问作者Yuriy Kirel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 03:48:35