使用 Java、Dozer 和 Hibernate 将 DTO 映射并更新到数据库

发布于 2024-10-27 08:44:14 字数 1121 浏览 2 评论 0原文

我正在使用 Dozer 将 Hibernate 实体映射到它们的 DTO。一个简化的示例类如下:

@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
  }

现在映射工作正常,但在尝试进行更新时遇到问题。假设我的数据库中有一个角色(1,“管理员”)。我的观点首先生成带有更新字段的 DTO:

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

最终,保存角色的类接收到 DTO 并通过 Dozer 将其转换为实体类以保存更改:

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

此时,我的角色实体已丢失其 ID,大概是因为ID列被定义为自动增量,我显然无法更新空ID实体。

那么我应该如何解决这个问题,以便 ID 和更新的字段都映射到实体呢?我总是可以使用 hibernate 引入实体对象,并使用 DTO 中的字段更新其每个字段并将其保存回来,但这会破坏使用 Dozer 的整个目的。

感谢您的任何帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独自唱情﹋歌 2024-11-03 08:44:14

在这种情况下,为 Role 实体上的 roleId 提供 setter 是一种完全有效的方法。 Dozer 随后也会设置 ID。 (另外,我假设您的 Role 字段并不是真正公开的。)

使用 Dozer,您可以从 DTO 创建一个实体。此时,该实体已分离,即不与 Hibernate 会话关联。然后,您应该使用 session.merge(role) 来保存更改。

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.

自由如风 2024-11-03 08:44:14
        @Autowired
        private RoleDao roleDao;

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

您可以创建 Role Dao 类并在管理器类中进行引用,您在哪里进行映射,并在 GenericDao 中创建更新方法来更新您的 Role 类,您在哪里定义了 hibernate 更新方法,调用它,您的工作就完成了。

        @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.

黯淡〆 2024-11-03 08:44:14

您不必执行合并。 Dozer 将允许您将更改应用到实体。

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文