hibernate中的createalias连接父表两次

时间:2018-05-08 10:07:53

标签: spring hibernate join criteria

我在部门和员工之间有一对一的双向。一个部门可以有多个员工。

我想使用hibernate标准加入部门和员工,所以我使用的是createalias方法。

Criteria criteriaDepartment = session.createCriteria(DepartmentEntity.class);
criteriaDepartment.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
criteriaDepartment.createAlias("employeeEntity", "emp",JoinType.LEFT_OUTER_JOIN);   
List<DepartmentEntity> list = criteriaDepartment.list();

但是,hibernate将department表与员工连接起来,然后再与department表连接。

hibernate生成的查询如下:

Hibernate: select this_.id as id1_1_2_, this_.name as name2_1_2_, emp1_.deptId as deptId7_2_4_, emp1_.id as id1_2_4_, emp1_.id as id1_2_0_, emp1_.address as address2_2_0_, emp1_.deptId as deptId7_2_0_, emp1_.password as password3_2_0_, emp1_.phoneno as phoneno4_2_0_, emp1_.type as type5_2_0_, emp1_.userid as userid6_2_0_, department4_.id as id1_1_1_, department4_.name as name2_1_1_ from Department4 this_ left outer join Userdetails4 emp1_ on this_.id=emp1_.deptId left outer join Department4 department4_ on emp1_.deptId=department4_.id

为什么Department表连接多次???有什么办法可以防止这种情况发生。我也必须使用一些限制。

然而,当我使用fetchmode它工作正常但是使用这种方法我无法使用别名。

Department class:
@Entity
@Table(name = "Department4")
public class DepartmentEntity {

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE) 
    public int id;
    public String name;


    @OneToMany(mappedBy= "departmentEntity", cascade= CascadeType.ALL, orphanRemoval = true)
    public Set<EmployeeEntity> employeeEntity = new HashSet<EmployeeEntity>();


    public Set<EmployeeEntity> getEmployeeEntity() {
        return employeeEntity;
    }
    public void setEmployeeEntity(Set<EmployeeEntity> employeeEntity) {
        this.employeeEntity = employeeEntity;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "DepartmentEntity [id=" + id + ", name=" + name + ", employeeEntity=" + employeeEntity + "]";
    }

}

员工类

@Entity
@Table(name="Userdetails4")

public class EmployeeEntity  {

@Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE) 
    private int id;
    private String userid;
    private String password;
    private String address;

    private long phoneno;
    private String type;



    @ManyToOne
    @JoinColumn(name = "deptId")
    private DepartmentEntity departmentEntity;




    @OneToMany(mappedBy = "employeeEntity",  cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<AddressEntity> addressEntity = new HashSet<AddressEntity>();


    public Set<AddressEntity> getAddressEntity() {
        return addressEntity;
    }


    public void setAddressEntity(Set<AddressEntity> addressEntity) {
        this.addressEntity = addressEntity;
    }


    public DepartmentEntity getDepartmentEntity() {
        return departmentEntity;
    }


    public void setDepartmentEntity(DepartmentEntity departmentEntity) {
        this.departmentEntity = departmentEntity;
    }


    public EmployeeEntity()
    {}



    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUserid() {
        return userid;
    }
    public void setUserid(String userid) {
        this.userid = userid;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public long getPhoneno() {
        return phoneno;
    }
    public void setPhoneno(long phoneno) {
        this.phoneno = phoneno;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }





    @Override
    public String toString() {
        return "EmployeeEntity [id=" + id + ", userid=" + userid + ", password=" + password + ", address=" + address
                + ", phoneno=" + phoneno + ", type=" + type + "]";
    }


}

Hibernate版本:5.2.1

提前致谢。

0 个答案:

没有答案
相关问题