当前位置: 首页 > 知识库问答 >
问题:

使用双向实体方法

徐晔
2023-03-14

我有双向映射(@OneToMany Hibernate)和额外的方法来确保两个对象链接。简单的例子:

@Setter
class ParentDto {
    List<ChildDto> childList;
}

@Setter
class ChildDto {
    String text;
}

@Setter
class Parent {

    List<Child> childList;

    public void addChild(Child child) {
        childList.add(child);
        child.setParent(this);
    }
}

@Setter
class Child {
    Parent parent;
    String text;
}

制图员:

@Mapper(componentModel = "spring")
public interface TestMapper {

Parent toEntity(ParentDto parentDto);
}

生成:

public class TestMapperImpl implements TestMapper {

@Override
public Parent toEntity(ParentDto parentDto) {
    if ( parentDto == null ) {
        return null;
    }

    Parent parent = new Parent();
    parent.setChildList( childDtoListToChildList( parentDto.getChildList() ) );

    return parent;
}

protected Child childDtoToChild(ChildDto childDto) {
    if ( childDto == null ) {
        return null;
    }

    Child child = new Child();
    child.setText( childDto.getText() );

    return child;
}

protected List<Child> childDtoListToChildList(List<ChildDto> list) {
    if ( list == null ) {
        return null;
    }

    List<Child> list1 = new ArrayList<Child>( list.size() );
    for ( ChildDto childDto : list ) {
        list1.add( childDtoToChild( childDto ) );
    }
    return list1;
}

主要问题:如何强制Mapalyt使用parent.addchild(...)来保持父级和子级列表之间的双向映射。

我有一个更复杂的结构,有多个嵌套的子级,所以要考虑可扩展性。

共有2个答案

公冶宏深
2023-03-14

经过长时间的寻找,我找到了迄今为止最好的解决方案。它不使用特殊方法,但允许您维护双向连接。

@AfterMapping
default void mapBidirectional(@MappingTarget Parent parent){
    List<Child> childList = parent.getChildList();
    if (childList != null) {
        childList.forEach(child -> child.setParent(parent));
    }
}

将会是

@Override
public Parent toEntity(ParentDto parentDto) {
    if ( parentDto == null ) {
        return null;
    }

    Parent parent = new Parent();

    parent.setChildList( childDtoListToChildList( parentDto.getChildList() ) );

    mapBidirectional( parent );

    return parent;
}

但最有可能的是,这个问题还有另一个解决方案,因为双向通信非常常见,而且这个解决方案的扩展性不好

而且它不能被拆分成几个*映射类,因为不能在@AfterMapping方法中使用生成的变量

蒙光华
2023-03-14

MapStruct有集合映射策略的概念。它允许您在映射它们时使用加法器。

e. g.

@Mapper(componentModel = "spring", collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED)
public interface TestMapper {

    Parent toEntity(ParentDto parentDto);
}
 类似资料:
  • 问题内容: 我正在使用Jackson将JPA模型序列化为JSON。 我有以下课程: 和 我正在使用POJO映射从模型序列化为JSON。当我序列化Parent对象时,我得到以下JSON: 但是当我序列化一个Child时,我得到以下JSON: 缺少对父级的引用。有办法解决吗? 问题答案: 我认为您必须在@JsonIdentityInfo和@ JsonBackReference / @ JsonMana

  • 我正在使用Jackson将我的JPA模型序列化到JSON中。 我有以下课程: 和 我正在使用POJO映射从模型序列化到JSON。当我序列化父对象时,我得到以下JSON: 但当我序列化一个子对象时,会得到以下JSON: 缺少对父级的引用。有没有办法解决这个问题?

  • 我正在创建一些restful Web服务,并使用Spring-Boot创建一个嵌入式tomcat容器。 其中一个要求是实现双向SSL。我一直在研究HttpSecurity对象,可以使用以下方法使其仅在SSL通道上运行webservices:- 我似乎找不到一种方法来使Web服务仅可供提供有效客户端证书的应用程序访问。 我只有SSL的基本知识,所以即使是正确方向的一般指针也将不胜感激。 正在部署的服

  • 本文向大家介绍vue使用Proxy实现双向绑定的方法示例,包括了vue使用Proxy实现双向绑定的方法示例的使用技巧和注意事项,需要的朋友参考一下 前言:vue3.0要用Proxy来实现双向绑定,因此先来尝试一下实现方法。 1 Object.defineProperty 实现 原来vue2的实现使用Object.defineProperty,监听set,但对于数组直接下标给数组设置值监听不了。 2

  • 问题内容: 我正在使用Jackson将JPA模型序列化为JSON。 我有以下课程: 和 我正在使用POJO映射从模型序列化为JSON。当我序列化Parent对象时,我得到以下JSON: 但是当我序列化一个Child时,我得到以下JSON: 缺少对父级的引用。有办法解决吗? 问题答案: 我认为您必须在@JsonIdentityInfo和@ JsonBackReference / @ JsonMana

  • 我的用户实体: 我的角色实体: 现在,在数据库中创建的相应中间表具有以下属性。