如何在Hibernate自联接中获得最高n级记录

时间:2017-03-30 08:09:30

标签: java hibernate

我有一个班级:

    @Entity
    @Table(name="EMPL_MSTR_CUR_V1")
    public class EmployeeMasterVO {
        private String emplId;
        private String name; 

        private String supervisorId;
        private EmployeeMasterVO supervisor;

        /**
         * @return the emplId
         */
        @Id
        @Column(name="emplid") 
        public String getEmplId() {
            return emplId;
        }
        /**
         * @param emplId the emplId to set
         */
        public void setEmplId(String emplId) {
            this.emplId = emplId;
        }
        /**
         * @return the name
         */
        @Column(name="name")
        public String getName() {
            return name;
        }

        /**
         * 
         * @param supervisorId
         */
        public void setSupervisorId(String supervisorId) {
            this.supervisorId = supervisorId;
        }

/**
     * 
     * @return String
     */
    @Column(name="SUPERVISOR_ID")
    public String getSupervisorId() {
        return supervisorId;
    }

        @ManyToOne(fetch = FetchType.LAZY)
        @JoinColumn( name = "SUPERVISOR_ID", insertable=false, updatable=false, nullable=true)
        @NotFound(action = NotFoundAction.IGNORE)
        public EmployeeMasterVO getSupervisor() {
            return supervisor;
        }
        public void setSupervisor(EmployeeMasterVO supervisor) {
            this.supervisor = supervisor;
        }

        //emplStatus - getter & setter
       // employeeGroup -getter & setter
    }

并将条件查询为:

 Criteria cri = getSPGSession().createCriteria(EmployeeMasterVO.class, "employeeMasterVO")
                    .createAlias("employeeMasterVO.supervisor", "supervisor",Criteria.LEFT_JOIN);
            cri.add(Restrictions.eq("employeeMasterVO.emplStatus", "3"));
            cri.add(Restrictions.ne("employeeMasterVO.employeeGroup", "C").ignoreCase());

empldtls = cri.list();

按预期获得结果 但是当我empldtls.get(0).getSupervisor()时,它再次包含主管等等。

所以我的问题是如何限制最多只能达到2级 如果我做empldtls.get(0).getSupervisor()我应该得到主管但是 当我empldtls.get(0).getSupervisor().getSupervisor()时,我不需要这个级别。我做了什么,任何帮助都是适当的。

0 个答案:

没有答案