Skip to content

Latest commit

 

History

History
97 lines (73 loc) · 2.77 KB

README.md

File metadata and controls

97 lines (73 loc) · 2.77 KB

ObjectMapper

CI Coverage Status Maven Central License: MIT

Maps an object to another via getters and setters methods

Features

  • Maps DTO to Model/Entity object
  • Maps Model/Entity to DTO object
  • Maps nested objects
  • Maps nested Lists, Sets and Maps
  • Can map any object to any object if it follows the restrictions
  • Caches objects getters and setters methods for improve performance

Restrictions

  • Maps only corresponding origin object field if it has getter method and destination object have corresponding setter method
  • Returned class should have accessible constructor without parameters
  • Getter and setter methods name should have more than 3 characters

Requirements

  • Java 1.7+

Examples

@Entity
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String name;
    private String email;
    private String password;
    private LocalDate birthday;
    @Column(name = "Curso_id")
    private int courseId;

    @ManyToOne
    @JoinColumn(name = "Curso_id", insertable = false, updatable = false)
    private Course course;

    // getters, setters, equals, hashCode
}
public class StudentDTO {

    private int id;
    private String name;
    private String email;
    private String password;
    private LocalDate birthday;
    private int courseId;

    // getters, setters, equals, hashCode
}

1. Maps DTO to Model

    StudentDTO studentDTO = new StudentDTO();
    studentDTO.setId(id);
    studentDTO.setName(name);
    studentDTO.setEmail(email);
    studentDTO.setPassword(password);
    studentDTO.setBirthday(birthday);
    studentDTO.setCourseId(courseId);

    Student student = ObjectMapper.map(studentDTO, Student.class);

2. Maps Model to DTO

    Student student = new Student();
    student.setId(id);
    student.setName(name);
    student.setEmail(email);
    student.setPassword(password);
    student.setBirthday(birthday);
    student.setCourseId(courseId);

    StudentDTO studentDTO = ObjectMapper.map(student, StudentDTO.class);

3. Maps List, Set and Map

    List<Discipline> disciplineList = execute(query, parameters);
    List<DisciplineDTO> disciplineDTOList = ObjectMapper.map(disciplineList, DisciplineDTO.class)