开发者

DTO mapping and updating to a Database with Java, Dozer and Hibernate

I'm using Dozer to map Hibernate entities to their DTOs. A simplified example class is the following:

@Entity
public class Role {

     @Id
     @GeneratedValue
     @Column(name="RoleId", nullable=false)
     public int roleId;

     @Column(name="RoleName", nullable=false)
     @NotEmpty
     public String roleName;

     //get + set  (not for the roleId since its autogenerated)
}

   public class RoleDTO {   

     private int roleId;
     private String roleName;

     public RoleDTO(int roleId, String roleName) {
          this.roleId = roleId;
          this.roleName = roleName;
     }

     public RoleDTO() {开发者_如何学编程}

     //gets + sets
  }

Now the mapping works fine, but I have a problem when trying to make an Update. Let's say I have a Role (1,"Administrator") in my database. My view first generates the DTO with the updated fields:

RoleDTO roleDTO = new RoleDTO(1, "admin");

Eventually the class that persists the Role receives the DTO and transforms it to an Entity class through Dozer to persist the changes:

Role role = DozerMapper.map(roleDTO,Role.class);    

At this point, my role entity has lost its ID, presumably because the ID column is defined as auto-increment, and I obviously can't update the null-ID entity.

So how should I approach this problem so that the ID and the updated fields get all mapped to the entity? I could always bring the entity object with hibernate and update each of its fields with the ones from the DTO and save it back, but it would defeat the whole purpose of using Dozer.

Thanks for any help.


In this case, it is a perfectly valid approach to provide a setter for your roleId on the Role entity. Dozer will then set the ID as well. (Also, I assume your fields on Role aren't really public.)

With Dozer, you create an entity from the DTO. At that point, the entity is detached, i.e. not associated with a Hibernate session. You should then use session.merge(role) to persist the changes.


        @Autowired
        private RoleDao roleDao;

        @Transactional(readOnly = false)
        public void updateRole(RoleDTO roleDTO) {
            Role role = beanMapper.map(roleDTO, Role.class);
            roleDao.update(role);
        }   

You can make Role Dao class and make reference in manager class where are you doing mapping and make update method for update your Role class in GenericDao where are you defined hibernate update method call it and your job is done.


You do not have to perform a merge. Dozer will allow you to apply the changes to the entity.

Role role = <entitymangaer>.find("id", Role.class);   
role = beanMapper.map(dto, role);
role.update();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜