如何基于现有类生成@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
相关产品推荐
相关产品推荐

