我正在使用micronaut-data-hibernate-jpa
JPA 关系来保存父实体和子实体,将多个子实体添加到父实体并使用
parentWorkflowRepository.update(parentWorkflowEntity)
但它不会生成 ChildWorkflowStateEntity Id 并且它返回为 null 生成的子 Id。如何在 repository.update(parent) 之后立即获取 childId。
@Entity
@Table(name = "Parent_Workflow")
public class ParentWorkflowEntity{
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "WFL_ID")
private List<ChildWorkflowStateEntity> wflStates = new ArrayList<>();
}
@Entity
@Table(name = "CHILD_WORKFLOW_STATE")
public class ChildWorkflowStateEntity{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private Long id;
}
public interface ParentWorkflowRepository extends CrudRepository<ParentWorkflowEntity, Long> {}
long method(ParentWorkflowEntity parentWorkflowEntity, List<Object> list){
for(Object o: list){
ChildWorkflowStateEntity child = new ChildWorkflowStateEntity()
parentWorkflowRepository.getWflStates().add(child);
}
parentWorkflowRepository.update(parentWorkflowEntity);
return parentWorkflowEntity.getWflStates().findFirst().getId();
}