如何将实体类中的外键Map到模型类(pojo)对象

mdfafbf1  于 2021-07-16  发布在  Java
关注(0)|答案(1)|浏览(306)

用户模型类:

@Component
public class UserModel {
    private Integer userId;
    private Integer roleId;
    private String firstName;
    private String email;
    private String password;

    public Integer getUserId() {
        return userId;
    }

    public Integer getRoleId() {
        return roleId;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getEmail() {
        return email;
    }

    public String getPassword() {
        return password;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public void setRoleId(Integer roleId) {
        this.roleId = roleId;
    }

    public void setName(String firstName) {
        this.firstName = firstName;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}

用户角色的模型类:

@Component
public class RolesModel {

    private Integer roleId;
    private String roleName;

    public Integer getRoleId() {
        return roleId;
    }

    public void setRoleId(Integer roleId) {
        this.roleId = roleId;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

}

用户的实体类:

@Entity
@Table(name = "userBloodBank")
public class User {

    @Id
    @GeneratedValue (strategy = GenerationType.AUTO)
    private Integer userId;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name="roleId")
    private Roles roles;

    @NotNull
    @Column
    @Pattern(regexp="[A-Z][a-zA-Z]*",message="Only enter words starting with captital letter")
    private String firstName;

    @NotNull
    @Column
    @Email
    @Pattern(regexp = "^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+$", message = "Enter a proper email ID")
    private String email;

    @NotNull
    @Column
    @Pattern(regexp= "^(?=.*[0-9])"
            + "(?=.*[a-z])(?=.*[A-Z])"
            + "(?=.*[@#$%^&+=])"
            + "(?=\\S+$).{8,20}$", message="Enter minimum 8 characters and maximum 20 characters")
    private String password;

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public Roles getRoles() {
        return roles;
    }

    public void setRoles(Roles roles) {
        this.roles = roles;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}

角色的实体类:

@Entity
@Table(name="Roles")
public class Roles {

    @Id
    @GeneratedValue (strategy = GenerationType.AUTO)
    private Integer roleId;

    @NotNull
    @Column
    @Pattern(regexp="(Recipient|Donor|BloodBankAdmin|Admin)", message = "Choose among Recipient|Donor|BloodBankAdmin|Admin")
    private String roleName;

    public Integer getRoleId() {
        return roleId;
    }
    public void setRoleId(Integer roleId) {
        this.roleId = roleId;
    }
    public String getRoleName() {
        return roleName;
    }
    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

}

在上面的例子中,角色id有onetoone关系,我如何将它Map到角色和用户的模型对象?帮助我编写创建函数“convertmodeltoentity()”和“convertentitytomodel”的代码。

mepcadol

mepcadol1#

您可以在用户实体类中添加以下代码以将实体Map到模型。

public static UserModel convertEntityToModel(User user) {
        UserModel userModel = new UserModel();

        userModel.setUserId(user.getUserId());
        userModel.setRoleId(user.getRoles().getRoleId());
        userModel.setFirstName(user.getFirstName());
        userModel.setEmail(user.getEmail());
        userModel.setPassword(user.getPassword());
        return userModel;
    }

这将返回用户模型。
我认为您应该在用户模型中添加角色模型(或角色列表模型)。

相关问题