java 在MapStructMap器中无法识别ObjectFactory方法-故障排除提示

edqdpe6u  于 2023-06-04  发布在  Java
关注(0)|答案(1)|浏览(444)

我有一个单一表继承的实体:

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorOptions(insert = false)
public abstract class Hierarchy implements Serializable {

    private HierarchyId id;

    protected Hierarchy() {
    }

    protected Hierarchy(final HierarchyId id) {
        this.id = id;
    }

    @EmbeddedId
    public HierarchyId getId() {
        return id;
    }

    public void setId(final HierarchyId id) {
        this.id = id;
    }

    @Embeddable
    public static class HierarchyId implements Serializable {

        private User parent;
        private User child;
        private String type;

        public HierarchyId() {
        }

        public HierarchyId(final User parent, final User child, final String type) {
            this.parent = parent;
            this.child = child;
            this.type = type;
        }

        @ManyToOne(fetch = FetchType.LAZY)
        public User getParent() {
            return parent;
        }

        public void setParent(final User parent) {
            this.parent = parent;
        }

        @ManyToOne(fetch = FetchType.LAZY)

        public User getChild() {
            return child;
        }

        public void setChild(final User child) {
            this.child = child;
        }

        @Column(name = "type", nullable = false, length = 1)
        public String getType() {
            return type;
        }

        public void setType(final String type) {
            this.type = type;
        }

    }

}

我正在尝试创建一个Map器,它可以将我的通用dtoMap到我的实体的一个特定子类:

@Configuration
@Mapper
public abstract class HierarchyMapper {

    @Autowired
    private UserService userService;

    @Mapping(target = "id.parent.id", ignore = true)
    @Mapping(target = "id.child.id", ignore = true)
    @Mapping(source = "type", target = "id.type")
    @BeanMapping(qualifiedByName = "afterHierarchyMapping")
    public abstract Hierarchy mapHierarchy(HierarchyDto hierarchy);

    @ObjectFactory
    public Hierarchy createHierarchy(HierarchyDto hierarchy) {
        if (SUBTYPE_A.equals(hierarchy.getType())) {
            return new SubtypeA();
        } else if (SUBTYPE_B.equals(hierarchy.getType())) {
            return new SubtypeB();
        }
        return null;
    }

    @AfterMapping
    @Named("afterHierarchyMapping")
    void afterHierarchyMapping(@MappingTarget Hierarchy hierarchy, HierarchyDto hierarchyDto) {
        hierarchy.getId()
                  .setParent(userService.getUserById(hierarchyDto.getIdParent()));
        hierarchy.getId()
                  .setChild(userService.getUserById(hierarchyDto.getIdChild()));
    }
}

我的问题是,使用这个Map器,MapStrsuct给我一个错误,说明:

java: The return type Hierarchy is an abstract class or interface. Provide a non abstract / non interface result type or a factory method.

所以我想知道我的@ObjectFactory是否真的被考虑在内,因为它应该是MapStruct正在寻找的工厂方法。也许是别的什么,但我看不出是什么。
我不认为这是相关的,但User也是一个抽象类,例如,如果我从mapHierarchy中删除@BeanMapping(qualifiedByName =“afterHierarchyMapping”),那么我会得到同样的错误,但User会得到同样的错误。

huwehgph

huwehgph1#

当我从Map器中删除所有这些行时,它终于工作了:

@Mapping(target = "id.parent.id", ignore = true)
@Mapping(target = "id.child.id", ignore = true)

@BeanMapping(qualifiedByName = "afterHierarchieMapping")

@Named("afterHierarchieMapping")

相关问题