简体   繁体   English

使用hibernate对一对多查询的数据库有多少?

[英]How many hit to database for one-to-many query using hibernate?

I am having one-to-many relationship between Employee and Department and my class is looking like : 我在Employee和Department之间有一对多的关系,我的班级看起来像:

@Entity
public class Employee {
@Id
private int empId;
private String empName;
@OneToMany
@JoinTable (name = "relationalTable" , joinColumns = @JoinColumn(name = "empId"),inverseJoinColumns=@JoinColumn(name = "deptId"))
private Collection <Department> dept = new ArrayList<Department>();

public int getEmpId() {
    return empId;
}
public void setEmpId(int empId) {
    this.empId = empId;
}

public Collection<Department> getDept() {
    return dept;
}
public void setDept(Collection<Department> dept) {
    this.dept = dept;
}


@Column 
public String getEmpName() {
    return empName;
}
public void setEmpName(String empName) {
    this.empName = empName;
}
}

and my department class like : 和我的系类一样:

@Entity
public class Department {
   int deptId;
   String deptName;
   private Employee emp;

   @ManyToOne
   public Employee getEmp() {
      return emp;
   }

   public void setEmp(Employee emp) {
      this.emp = emp;
   }

   @Id
   public int getDeptId() {
      return deptId;
   }

   public void setDeptId(int deptId) {
      this.deptId = deptId;
   }

   @Column
   public String getDeptName() {
      return deptName;
   }

   public void setDeptName(String deptName) {
      this.deptName = deptName;
   }
}

If I fetch the Department. 如果我拿到部门。 So it will fetch all the employee at the same time associated with it. 因此,它将在与其相关联的同时获取所有员工。 say with department id 1 there are 1000 employee. 用部门ID 1说有1000名员工。 So how many queries will execute at this time to database to fetch all the data? 那么有多少查询会在此时执行到数据库以获取所有数据?

IF the fetchType was Lazy Hibernate retrieve the record when you try to access the departments 如果fetchType为Lazy Hibernate,则在您尝试访问部门时检索记录

If its eager Hibernate join all the queries into one queries and execute one query. 如果其渴望的Hibernate将所有查询加入一个查询并执行一个查询。

If you haven't specified the fetch type, then one-to-many relationships are by default lazy loaded. 如果尚未指定fetch类型,则默认延迟加载一对多关系。 so your Employee class has one to many Departments. 所以你的Employee类有一个到多个部门。

so if you u load 1000 Employees, then there will be 1000 calls to load departments (1 call per each Employee). 因此,如果您加载1000个员工,那么将有1000个负载部门的呼叫(每个员工1个呼叫)。

what i cant understand your relationships,in this organization one department can have only one Employee but one Employee can work on few departments which is strange. 我无法理解你的关系,在这个组织中,一个部门只能有一个员工,但一个员工可以在几个奇怪的部门工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM