【问题标题】:DTO to JPA Entity in onetomany bidirectional relationship with orika与 orika 的单对多双向关系中的 DTO 到 JPA 实体
【发布时间】:2021-07-23 07:10:33
【问题描述】:

我正在将传入的 DTO 对象复制到我的具有双向 OneToMany 关系的 JPA 实体,使用 orika 库(尝试了不同的其他库,如 Dozer、SpringBeanUtils 等,都具有相同的效果),虽然复制工作正常,但持久实体没有更新外键子实体。我知道这是由于缺少与父实体的子实体同步而发生的。

但是使用 orika 或任何类似库的整个想法是避免单独复制每个实体/对象的样板代码。所以我想知道我是否可以在复制过程中进行同步?

以下是我的实体和 DTO 类

@Entity
@Data
public class Parent
{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String name;
@OneToMany(mappedBy=“parent”,cascade=CascadeType.ALL,OrphanRemoval=true)
private List<Child> childs;
public void addChild(Child child)
{
childs.add(child);
child.setParent(this);
}
public void removeChild(Child child)
{
childs.remove(child);
child.setParent(null);
}
}


@Entity
@Data
public class Child
{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String name;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name=“parent_id”)
private Parent parent;
}

@Data
public class ParentDto
{
private Long id;
private String name;
private List<ChildDto> childs;
}

@Data
public class ChildDto
{
private Long id;
private String name;
}

和 Orkia 复制逻辑 -

DefaultMapperFactory factory=new DefaultMapperFactory().Builder().build();
factory.classMap(ParentDto.class,Parent.class).byDefault().register();
Parent parentEntity=factory.getMapperFacade().map(parentDtoObject,Parent.class);
//parentEntity.getChilds().forEach(child -> child.setParent(parentEntity));
repository.save(parentEntity);

当repository.save() 完成时,子表parent_id 的foreign_key 被持久化为null 而不是实际值。但是如果我取消注释该行注释它工作正常,我不想这样做,因为我的实体有很多子对象,并且循环会对性能产生影响,并且代码看起来也很丑陋。有没有更好的方法来做到这一点?还是单向 OneToMany 会起作用?

【问题讨论】:

    标签: java spring-boot hibernate spring-data-jpa orika


    【解决方案1】:

    您可以使用 Blaze-Persistence Entity-Views 来做到这一点,它的设计考虑了这一点。

    我创建了该库以允许在 JPA 模型和自定义接口或抽象类定义模型之间轻松映射,例如 Spring Data Projections on steroids。这个想法是您按照自己喜欢的方式定义目标结构(域模型),并通过 JPQL 表达式将属性(getter)映射到实体模型。

    使用 Blaze-Persistence Entity-Views 的用例的 DTO 模型可能如下所示:

    @EntityView(Parent.class)
    @UpdatableEntityView
    public interface ParentDto {
        @IdMapping
        Long getId();
        String getName();
        void setName(String name);
        @UpdatableMapping
        Set<ChildDto> getChilds();
    
        @EntityView(Child.class)
        @UpdatableEntityView
        interface ChildDto {
            @IdMapping
            Long getId();
            String getName();
            void setName(String name);
        }
    }
    

    查询是将实体视图应用于查询的问题,最简单的就是通过 id 进行查询。

    ParentDto a = entityViewManager.find(entityManager, ParentDto.class, id);

    Spring Data 集成让您可以像使用 Spring Data Projections 一样使用它:https://persistence.blazebit.com/documentation/entity-view/manual/en_US/index.html#spring-data-features

    Page<ParentDto> findAll(Pageable pageable);
    

    最好的部分是,它只会获取实际需要的状态!

    保存状态也很简单:

    entityViewManager.save(entityManager, parentDto);
    

    【讨论】:

      猜你喜欢
      • 2021-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-23
      • 1970-01-01
      • 1970-01-01
      • 2021-08-08
      相关资源
      最近更新 更多