使用 Java、Dozer 和 Hibernate 将 DTO 映射并更新到数据库
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这种情况下,为
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 theRole
entity. Dozer will then set the ID as well. (Also, I assume your fields onRole
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.您可以创建 Role Dao 类并在管理器类中进行引用,您在哪里进行映射,并在 GenericDao 中创建更新方法来更新您的 Role 类,您在哪里定义了 hibernate 更新方法,调用它,您的工作就完成了。
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.
您不必执行合并。 Dozer 将允许您将更改应用到实体。
You do not have to perform a merge. Dozer will allow you to apply the changes to the entity.