Java ModelMapper-无法将java.util.List转换为java.util.List

时间:2018-12-20 15:38:11

标签: java modelmapper

我正在使用Java ModelMapper库将DTO映射到ORM @Entity对象。我设置了以下测试:

    public class MapperTest {

    @Autowired
    private ModelMapper mapper;

    @Autowired
    private TicketRepository ticketRepo;

    @Test
    public void testTicket() {
        Ticket ticket = ticketRepo.findById(4).get();

        TicketDTO dto = mapper.map(ticket, TicketDTO.class);
        assertThat(dto.getEquipmenDescription()).isEqualTo(ticket.getEquipment().getDescription());
        assertThat(dto.getEquipmentNotes()).isEqualTo(ticket.getEquipmentNotes());
        assertThat(dto.getId()).isEqualTo(ticket.getId());
        assertThat(dto.getNotes()).isEqualTo(ticket.getNotes());
        assertThat(dto.getOracleID()).isEqualTo(ticket.getOracleID());
        assertThat(dto.getPropertyID()).isEqualTo(ticket.getPropertyID());
        assertThat(dto.getNotes().size()).isEqualTo(ticket.getNotes().size());

        for (TicketNoteDTO note : dto.getNotes()) {
            assertThat(note.getId()).isNotEqualTo(0);
            assertThat(note.getIssueDate()).isNotNull();
            assertThat(note.getUserUserName()).isNotEmpty();
        }
    }

}

此操作失败,并显示以下错误:

org.modelmapper.MappingException: ModelMapper mapping errors:

1) Converter org.modelmapper.internal.converter.CollectionConverter@501c6dba failed to convert java.util.List to java.util.List.

以下是我的实体和相应的DTO。为简便起见,省略了getter和setter。

门票

   @Entity
    @Table(name = "ticket")
    public class Ticket {

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id")
        private int id;

        @Column(name = "equipment_notes")
        private String equipmentNotes;

        @Column(name = "is_open")
        private boolean isOpen;

        @Column(name = "oracle_id")
        private String oracleID;

        @ManyToOne
        @JoinColumn(name = "equipment_id")
        private EquipmentCategory equipment;

        @Column(name = "property_id")
        private String propertyID;

        @OneToMany(mappedBy = "ticket")
        private List<TicketNote> notes = new ArrayList<>();

}

TicketNote(为简便起见,省略了getter和setters)

    @Entity
    @Table(name = "ticket_note")
    public class TicketNote {

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id")
        private int id;

        @ManyToOne
        @JoinColumn(name = "ticket_id")
        private Ticket ticket;

        @ManyToOne
        @JoinColumn(name = "user_id")
        private AppUser user;

        @Column(name = "issue_date")
        private LocalDate issueDate;

        @Column(name = "oracle_contact")
        private String oracleContact;

        @Column(name = "issue_resolved")
        private boolean issueResolved;
}

TicketDTO

    public class TicketDTO {

        private int id;
        private String equipmentNotes;
        private boolean isOpen;
        private String oracleID;
        private String equipmenDescription;
        private String propertyID;
        private List<TicketNoteDTO> notes = new ArrayList<>(); 
}

TicketNoteDTO

public class TicketNoteDTO {

        private int id;
        private String userUserName;
        private LocalDate issueDate;
        private String oracleContact;
        private boolean issueResolved;
    }

我对ModelMapper库有一些经验,但是我不确定是什么问题。任何建议表示赞赏。

谢谢。

0 个答案:

没有答案