4

I have been shifting my project from spring to Spring Boot and introducing embedded tomcat there were some adaptations i have to do Only issue i am facing in my Old project i have Mapper interface.

and when i compile project it start giving error

Error:(12, 5) java: No property named "schoolId" exists in source parameter(s).

I have search alot about it some said lombok dependency placement in pom i tried placing it before and after map-struct dependency but error is still here

Following is my class

enter image description here

Following is error i am facing

enter image description here

6
  • Did you remove the generated-source in the target folder? @Shermeen Commented Jan 24, 2020 at 12:43
  • @Shermeen Not sure but as per my knowledge 'target' pointed to entity and you're defining column instead of that so it gives an error. Try this, @Mapping(source = "schoolId", target="school") Commented Jan 24, 2020 at 12:43
  • @Hatice i have tried removing target folder as well but not working Commented Jan 24, 2020 at 13:18
  • @Dhwani its not working as well :( Commented Jan 24, 2020 at 13:21
  • 1
    @Shermeen Why did you put school.schoolId for target field? If this is correct, why the other target field is just schoolName instead of school.schoolName Please, change the images with code. Could you try groupDto.schoolId for source field and target just schoolId? Commented Jan 24, 2020 at 14:12

3 Answers 3

10

I think you should re-order the lombok and mapstruct dependencies in the pom.xml file and it works fine.

Strict ordering should be like this

  1. lombok
  2. mapstruct

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

@Shermeen Can you share your configured code?

So we cross verify it. Still i add this example please check it.

Structure

enter image description here

EntityMapper.java

public interface EntityMapper<D, E> {
  E toEntity(D dto);
  D toDTO(E entity);
  List<E> toEntity(List<D> dtoList);
  List<D> toDTO(List<E> entityList);
}

UserMapper.java

import com.ecommerce.auth_service.domain.external_db.User;
import com.ecommerce.auth_service.domain.external_db.dto.UserDTO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

import java.util.UUID;

@Mapper(componentModel = "spring", uses = {UserMapper.class})
public interface UserMapper extends EntityMapper<UserDTO, User> {

  @Mapping(source = "user_id", target = "user")

  default User fromId(UUID id) {
    if (id == null)
      return null;
    User user = new User();
    user.setUserId(id);
    return user;
  }
}

UserMapperImpl.java

import com.ecommerce.auth_service.domain.external_db.User;
import com.ecommerce.auth_service.domain.external_db.dto.UserDTO;
import com.ecommerce.auth_service.domain.external_db.mapper.UserMapper;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class UserMapperImpl implements UserMapper {
  @Override
  public User toEntity(UserDTO dto) {
    if (dto == null)
      return null;

    User user = new User();
    user.setUserId(dto.getUserId());
    user.setUsername(dto.getUsername());
    user.setEmail(dto.getEmail());
    user.setPassword(dto.getPassword());
    user.setPhoneNo(dto.getPhoneNumber());
    user.setStatus(dto.getStatus());
    user.setStore(dto.getStoreId());
    user.setAuthority(dto.getAuthority());
    user.setPrivileges(dto.getAuthority().getPrivileges());

    return user;
  }

  @Override
  public UserDTO toDTO(User entity) {
    if (entity == null) {
      return null;
    }

    UserDTO userDTO = new UserDTO();

    userDTO.setUserId(entity.getUserId());
    userDTO.setUsername(entity.getUsername());
    userDTO.setEmail(entity.getEmail());
    userDTO.setPassword(entity.getPassword());
    userDTO.setPhoneNumber(entity.getPhoneNo());
    userDTO.setStatus(entity.getStatus());
    userDTO.setStoreId(entity.getStore());

    entity.getAuthority().setPrivileges(entity.getPrivileges());
    userDTO.setAuthority(entity.getAuthority());

    userDTO.setCreatedBy(entity.getCreatedBy());
    userDTO.setCreatedDate(entity.getCreatedDate());
    userDTO.setModifiedBy(entity.getModifiedBy());
    userDTO.setModifiedDate(entity.getModifiedDate());

    return userDTO;
  }

  @Override
  public List<User> toEntity(List<UserDTO> dtoList) {
    if (dtoList == null || dtoList.isEmpty())
      return null;
    List<User> userList = new ArrayList<>(dtoList.size());
    for (UserDTO userDTO : dtoList) {
      userList.add(toEntity(userDTO));
    }
    return userList;
  }

  @Override
  public List<UserDTO> toDTO(List<User> entityList) {
    if (entityList == null)
      return null;
    List<UserDTO> userDTOList = new ArrayList<>(entityList.size());
    for (User user : entityList) {
      userDTOList.add(toDTO(user));
    }
    return userDTOList;
  }
}

1 Comment

i dont know how can i share my configured code on stack over flow apparently code looks fine dont know why its not recognizing source properties.
0

I had the most wired experience about it. in my case, I didn't declare any annotationProcessorPaths in my pom.xml, I just added dependencies in my pom like this:

        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${mapstruct.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>${mapstruct.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

by only re-ordering it and moving up the lombok dependency the problem fixed :|

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${mapstruct.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>${mapstruct.version}</version>
        </dependency>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.