Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java MapStruct:在映射到另一个对象列表时获取对象列表中的空值_Java_Mapstruct - Fatal编程技术网

Java MapStruct:在映射到另一个对象列表时获取对象列表中的空值

Java MapStruct:在映射到另一个对象列表时获取对象列表中的空值,java,mapstruct,Java,Mapstruct,当映射到另一个对象列表时,我在对象列表中获得空值 Dto类:(考虑在Dto类中生成的getter/setter) 测试等级: public class App { public static void main(String[] args) { CustomerDto customerDto = new CustomerDto(); customerDto.id = 10L; customerDto.customerName = "Fi

当映射到另一个对象列表时,我在对象列表中获得空值

Dto类:(考虑在Dto类中生成的getter/setter)

测试等级:

 public class App {
    public static void main(String[] args) {

        CustomerDto customerDto = new CustomerDto();
        customerDto.id = 10L;
        customerDto.customerName = "Filip";
        OrderItemDto order1 = new OrderItemDto();
        order1.name = "Table";
        order1.quantity = 2L;

        ArrayList<OrderItemDto> tmpOrders = new ArrayList<OrderItemDto>();
        tmpOrders.add(order1);
        customerDto.setOrders(tmpOrders);

        for (OrderItemDto order : customerDto.getOrders()) {
            System.out.println(" order items = " + order.toString());
        }

        Customer customer = CustomerMapper.MAPPER.toCustomer(customerDto);

        System.out.println(" id ==== " + customer.getId());
        System.out.println("getName == " + customer.getName());

        for (OrderItem order : customer.getOrderItems()) {
            System.out.println(" order items = " + order.toString());
        }
    }
}
这里我得到订单对象的空值(名称、数量)


我不明白这段代码中缺少了什么。

问题解决了,我清理了项目,然后用mvn安装并测试了构建。它适合我。根本原因是,我使用的是mapstruct版本1.0.0-final,然后我将版本更改为1.1.0-final,因此可以使用注释及其属性。问题得到解决,我清理了项目,然后使用mvn安装并测试了构建。根本原因是,我使用的是mapstruct版本1.0.0-final,然后我将版本更改为1.1.0-final,因此注释及其属性可用。
    @Mapper(uses = { OrderItemMapper.class })
public interface CustomerMapper {

    CustomerMapper MAPPER = Mappers.getMapper( CustomerMapper.class );

    @Mappings({
        @Mapping(source = "orders", target = "orderItems"),
        @Mapping(source = "customerName", target = "name")
    })
    Customer toCustomer(CustomerDto customerDto);

    @InheritInverseConfiguration
    CustomerDto fromCustomer(Customer customer);
}

    @Mapper
public interface OrderItemMapper {

    OrderItemMapper MAPPER = Mappers.getMapper(OrderItemMapper.class);

    @Mappings({
        @Mapping(source = "name", target = "name"),
        @Mapping(source = "quantity", target = "quantity")
    })
    OrderItem toOrder(OrderItemDto orderItemDto);

    @InheritInverseConfiguration
    OrderItemDto fromOrder(OrderItem orderItem);
}
 public class App {
    public static void main(String[] args) {

        CustomerDto customerDto = new CustomerDto();
        customerDto.id = 10L;
        customerDto.customerName = "Filip";
        OrderItemDto order1 = new OrderItemDto();
        order1.name = "Table";
        order1.quantity = 2L;

        ArrayList<OrderItemDto> tmpOrders = new ArrayList<OrderItemDto>();
        tmpOrders.add(order1);
        customerDto.setOrders(tmpOrders);

        for (OrderItemDto order : customerDto.getOrders()) {
            System.out.println(" order items = " + order.toString());
        }

        Customer customer = CustomerMapper.MAPPER.toCustomer(customerDto);

        System.out.println(" id ==== " + customer.getId());
        System.out.println("getName == " + customer.getName());

        for (OrderItem order : customer.getOrderItems()) {
            System.out.println(" order items = " + order.toString());
        }
    }
}
order items = name = Table , qty = 2
id ==== 10
getName ==Filip

order items = name = null , qty = null