在多对多关系的情况下获取结果

时间:2015-11-28 10:56:17

标签: java hibernate many-to-many hibernate-mapping foreign-key-relationship

我有三张桌子

  1. 员工:存储员工信息

  2. role_description:存储我喜欢的角色类型管理员,区域经理,国家/地区经理

  3. employee_role_details:员工和角色描述与一些额外字段之间存在多对多的关系。
  4. 现在我在从employee_role_details中获取来自employee表的(employee_id,email),(role_ownership,role_id)的结果以及来自role_description中与role_id相对应的role_description的(role_name和role_description)时遇到问题。

    Employee.java

    @Entity
    @Table(name = "employee", uniqueConstraints= @UniqueConstraint(columnNames={"employee_email"}))
    public class Employee {
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "employee_id")
        private long employeeId;
    
        @Column(name = "employee_name")
        private String employeeName;
    
        @Column(name = "employee_email", unique = true, nullable = false)
        private String employeeEmail;
    
        @Column(name = "contact_no")
        private String contactNo;
    
        @Column(name = "password")
        private String password;
    
        @Column(name = "is_active")
        private Boolean isActive = true;
    
        @Transient
        private long EmployeeRoleId;
    
        @Transient
        private String roleName;
    
        @Transient
        private int totalpage;
    
        @OneToMany(mappedBy = "employee", cascade = CascadeType.ALL)
        //@JsonIgnore
        @JsonManagedReference(value = "employeeRoleRecord-employee")
        @LazyCollection(LazyCollectionOption.FALSE)
        private List<EmployeeRoleRecord> roleRecords = new ArrayList<>();
    
        @OneToMany(mappedBy = "assignedTo", cascade = CascadeType.ALL)
        @JsonManagedReference(value = "complaint-treatment-employee")
        @LazyCollection(LazyCollectionOption.FALSE)
        private List<ComplaintsCategory> complaintCategoryAssignedTo = new ArrayList<>();
    

    EmployeeDescription.java

    @Entity
    @Table(name = "role_description")
    public class RoleDescription {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "role_id")
        private long roleId;
    
        @Column(name = "role_name")
        private String roleName;
    
        @Column(name = "role_description")
        private String roleDescription;
    
        @Column(name = "status")
        private boolean status;
    
        @OneToMany(mappedBy = "description", cascade = CascadeType.ALL)
        @JsonManagedReference(value = "employeeRoleRecord-roleDescription")
        @LazyCollection(LazyCollectionOption.FALSE)
        private List<EmployeeRoleRecord> roleRecords = new ArrayList<EmployeeRoleRecord>();
    

    EmployeeRoleRecord

    @Entity
    @Table(name = "employee_role_record")
    public class EmployeeRoleRecord {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "employee_role_id")
        private long employeeRoleId;
    
        @Column(name = "role_ownership")
        private String roleOwnership;
    
        @Column(name = "status")
        private boolean status = true;
    
    
        @ManyToOne(cascade=CascadeType.ALL)
        @JsonBackReference(value = "employeeRoleRecord-employee")
        @JoinColumn(name = "employee_id")
        private Employee employee;
    
        @ManyToOne(cascade=CascadeType.ALL)
        @JsonBackReference(value = "employeeRoleRecord-roleDescription")
        @JoinColumn(name = "role_id")
        private RoleDescription description;
    
        @Transient
        private long roleId;
    

    dao中的代码

    public Employee getEmployeeByEmail(String email) {
            return (Employee) sessionFactory.getCurrentSession()
                    .createCriteria(Employee.class)
                    .add(Restrictions.eq("employeeEmail", email)).uniqueResult();
        }
    

    服务:

    public Employee getSurveyData(String employeeEmail){
    
            Employee loggedInEmployee =
                     loginDao.getEmployeeByEmail(employeeEmail);
    
            Employee  loggedInEmployeeRecord = new Employee();
    
                loggedInEmployeeRecord.setEmployeeId(loggedInEmployee.getEmployeeId());
                loggedInEmployeeRecord.setEmployeeName(loggedInEmployee.getEmployeeName());
    
                List<EmployeeRoleRecord> employeeRoleRecords = loggedInEmployee.getRoleRecords();
    
                List<EmployeeRoleRecord> employeeRoleRecord = new  ArrayList<>();
    
                for(EmployeeRoleRecord record :  employeeRoleRecords){
    
                    EmployeeRoleRecord employeeRole = new EmployeeRoleRecord();
    
                    employeeRole.setRoleId(record.getDescription().getRoleId());
                    employeeRole.setRoleName(record.getDescription().getRoleName());
                    employeeRole.setRoleOwnership(record.getRoleOwnership());
                    employeeRole.setStatus(record.isStatus());
                    employeeRoleRecord.add(employeeRole);
                }
    
                loggedInEmployeeRecord.setRoleRecords(employeeRoleRecord);
    
    
             return loggedInEmployee;
        }
    

    输出:

        {
      "employeeId": 1,
      "employeeName": "Dhyanandra Singh",
      "employeeEmail": "admin",
      "contactNo": "9893651872",
      "password": "123456",
      "isActive": true,
      "roleRecords": [
        {
          "employeeRoleId": 1,
          "roleOwnership": "overall project",
          "status": true,
          "roleId": 0,
          "roleName": null
        }
      ],
    }
    

    问题:

    json输出我在角色记录列表中没有包含角色ID和角色名称。

2 个答案:

答案 0 :(得分:1)

问题是:

EmployeeRoleRecord classe上的roleId属性被映射为@Transient,这意味着它不会持久存储在数据库中,因此Hibernate无法获取此字段的结果。

看看你的课程建模我无法弄清楚你想要用roleId确切绘制什么,我建议采用两种可能的方法:

  1. 不要直接在getEmployeeByEmail方法上返回您的查询。首先得到结果,然后迭代它并在每条记录上手动设置所需的roleId;

  2. 检查您的模型方案并为roleId制作正确的映射,它可能应该引用另一个实体,因此请确保正确映射关系以在单个查询中获取结果。

  3. 祝你好运。

答案 1 :(得分:0)

在EmployeeRoleRecord中添加了临时roleId和roleName,然后在服务中进行了一些修改。比如迭代列表设置roleId和roleName。

public Employee getSurveyData(String employeeEmail){

            Employee loggedInEmployee =
                     loginDao.getEmployeeByEmail(employeeEmail);

            Employee  loggedInEmployeeRecord = new Employee();

                loggedInEmployeeRecord.setEmployeeId(loggedInEmployee.getEmployeeId());
                loggedInEmployeeRecord.setEmployeeName(loggedInEmployee.getEmployeeName());
                loggedInEmployeeRecord.setEmployeeEmail(loggedInEmployee.getEmployeeEmail());
                loggedInEmployeeRecord.setContactNo(loggedInEmployee.getContactNo());

                List<EmployeeRoleRecord> employeeRoleRecords = loggedInEmployee.getRoleRecords();

                List<EmployeeRoleRecord> employeeRoleRecord = new  ArrayList<>();

                for(EmployeeRoleRecord record :  employeeRoleRecords){

                    EmployeeRoleRecord employeeRole = new EmployeeRoleRecord();

                    employeeRole.setEmployeeRoleId(employeeRole.getEmployeeRoleId());
                    employeeRole.setRoleId(record.getDescription().getRoleId());
                    employeeRole.setRoleName(record.getDescription().getRoleName());
                    employeeRole.setRoleOwnership(record.getRoleOwnership());
                    employeeRole.setStatus(record.isStatus());
                    employeeRoleRecord.add(employeeRole);
                }

                loggedInEmployeeRecord.setRoleRecords(employeeRoleRecord);


             return loggedInEmployeeRecord;
        }