I use Dozer to map Hibernate objects to their DTO. A simplified sample class is as follows:
@Entity public class Role { @Id @GeneratedValue @Column(name="RoleId", nullable=false) public int roleId; @Column(name="RoleName", nullable=false) @NotEmpty public String roleName;
public class RoleDTO { private int roleId; private String roleName; public RoleDTO(int roleId, String roleName) { this.roleId = roleId; this.roleName = roleName; } public RoleDTO() {}
Now the display works fine, but I have a problem trying to update. Let's say I have a role (1, "Administrator") in my database. My view first generates a DTO with updated fields:
RoleDTO roleDTO = new RoleDTO(1, "admin");
In the end, the class that is saved in the role receives the DTO and converts it to the Entity class through Dozer to save the changes:
Role role = DozerMapper.map(roleDTO,Role.class);
At this point, my role object lost its identifier, apparently because the identifier column is defined as auto-increment, and I obviously cannot update the object with a null identifier.
So, how do I approach this problem so that the identifier and updated fields display all the objects? I could always bring the entity object to sleep mode and update each of my fields with the ones in the DTO and save it back, but this will defeat the whole purpose of using Dozer.
Thanks for any help.
source share