将列表源映射到目标

时间:2018-02-15 19:40:28

标签: spring-boot mapstruct

我有一个包含联系人对象列表的对象组织。 conacts列表已存在于数据库中。

要使用邮递员进行测试,当我需要添加组织时,我必须添加联系人ID列表

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "contact_organization", joinColumns =  @JoinColumn(name = "contacts_id", referencedColumnName = "id"),  inverseJoinColumns = @JoinColumn(name = "organizations_id",  referencedColumnName = "id"))
private Set<Organization> organizations = new HashSet<>();

在联系课中我有

@ManyToMany(mappedBy = "organizations")
private Set<Contact> contacts = new HashSet<>();

在组织课程中我有

private Set<Long> contactsId = new HashSet<>();

并且在organisationDTO课程中我有

@Mapper(componentModel = "spring", uses = { ContactMapper.class }))
public interface OrganizationMapper extends EntityMapper<OrganizationDTO, Organization> {

  @Mapping(source = "contacts", target = "contactsId")
  OrganizationDTO toDto(Organization organization);

  @Mapping(source = "contactsId", target = "contacts")
  Organization toEntity(OrganizationDTO organizationDTO);

  default Organization fromId(Long id) {
    if (id == null) {
     return null;
    }
    Organization organization = new Organization();
    organization.setId(id);
    return organization;
  }

  default Long fromContact(Contact contact) {
    return contact == null ? null : contact.getId();
  }
}

 @Mapper(componentModel = "spring", uses = { OrganizationMapper.class })
public interface ContactMapper extends EntityMapper<ContactDTO, Contact> {

 ContactDTO toDto(Contact contact);

 Contact toEntity(ContactDTO contactDTO);

 default Contact fromId(Long id) {
   if (id == null) {
    return null;
   }
   Contact contact = new Contact();
   contact.setId(id);
   return contact;
 }
}

在映射类中,我以这种方式进行了映射,但它似乎没有工作

Organization organization = organizationMapper.toEntity(organizationDTO);
for(Contact item : organization.getContacts()) {
  log.info("******************************************" + item.getId());
  log.info("++++++++++++++++++++++++++++++++++++++++++" + item.getLabel());
}
organization = organizationRepository.save(organization);

这里的问题它向我显示了id和标签为null,并且在数据库中它添加了组织但没有添加联系人

http://download.oracle.com/otn/nt/oracle12c/122010/win32_12201_client.zip

1 个答案:

答案 0 :(得分:0)

目前不支持此功能。您正尝试将列表Airbrake.configure do |config| config.ignore << 'RetryNotAnError' end 中的媒体资源id映射到contacts列表。

为了实现您的期待,您需要提供从contactsIdContact的映射方式。 您的映射器可能如下所示:

Long
相关问题