Spring boot 多通始终运行查询(始终渴望)
嗨,我正在使用SpringDataJPA(hibernate)和SpringBoot 我有两门课 公司------>员工 与每个公司有双向关系,有多个emp(从公司到员工的一个办公室) 公司实体Spring boot 多通始终运行查询(始终渴望),spring-boot,spring-data,spring-data-jpa,Spring Boot,Spring Data,Spring Data Jpa,嗨,我正在使用SpringDataJPA(hibernate)和SpringBoot 我有两门课 公司------>员工 与每个公司有双向关系,有多个emp(从公司到员工的一个办公室) 公司实体 public class Company implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue private int id;
public class Company implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private int id;
@Column(name="cmp_id")
private int cmpId;
@Column(name="company_name")
private String companyName;
@OneToMany(fetch=FetchType.LAZY, mappedBy="company")
private Set<Employee> employee;
... Getters & Setters
}
@Entity
@Table(name="employee")
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private int id;
@Column(name="emp_id")
private int empId;
@Column(name="emp_name")
private String empName;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="cmp_id", referencedColumnName="cmp_id")
@JsonIgnore
private Company company;
.... Getters & Setters ...
}
公司服务
public class CompanyService {
@Autowired
private CompanyRepository companyRepo;
public Company fetchCompany(int cmpId){
return companyRepo.findByCmpId(cmpId);
}
}
公司回购
public interface CompanyRepository extends JpaRepository<Company, Integer>{
public Company findByCmpId(int cmpId);
}
最后3个查询是多通连接的结果。虽然我已经提到了它的懒惰,但它仍然是工作的渴望。如何更改代码以使其变为惰性(基本上我需要停止触发这3个查询)。这似乎不可取,但惰性会根据需要加载实体。请评论一下
Set<Employee> ee = cmp.getEmployee();
for(Employee e : ee){
System.out.println(e.getEmpName());
}
Set ee=cmp.getEmployee();
对于(员工e:ee){
System.out.println(e.getEmpName());
}
并检查员工是否仍在加载。我认为这就是日志的原因如果我不加评论,它可以正常工作,但是如果需要员工的名字呢?@AnkitBansal,因为懒惰意味着它可以按需获取。如果调用cmp.getEmployee()。然后它会带来员工,这就是你需要的,除非你得到了员工,否则你无法得到员工的名字。您还可以编写另一个方法来获取员工,具体取决于公司id:)。我想问的是,既然lazy是按需获取的,那么员工到公司是懒惰的,那么为什么在我执行getEmpName()时会触发最后3个查询呢。它应该只触发一个员工查询?不是吗?不,当调用cmp.getEmployee()时,它将获取所有相关的雇员。如果你想得到一个特定的员工。在JpaRepository接口EBTW中编写一个特定的@query方法,我要求您注释掉代码,只是为了让您理解它,请查找@query以获取更多信息,否则您的代码会执行要求执行的操作
Hibernate: select company0_.id as id1_1_, company0_.cmp_id as cmp_id2_1_, company0_.company_name as company_3_1_ from company company0_ where company0_.cmp_id=?
Hibernate: select employee0_.cmp_id as cmp_id4_2_0_, employee0_.id as id1_2_0_, employee0_.id as id1_2_1_, employee0_.cmp_id as cmp_id4_2_1_, employee0_.emp_id as emp_id2_2_1_, employee0_.emp_name as emp_name3_2_1_ from employee employee0_ where employee0_.cmp_id=?
Hibernate: select company0_.id as id1_1_0_, company0_.cmp_id as cmp_id2_1_0_, company0_.company_name as company_3_1_0_ from company company0_ where company0_.cmp_id=?
Hibernate: select company0_.id as id1_1_0_, company0_.cmp_id as cmp_id2_1_0_, company0_.company_name as company_3_1_0_ from company company0_ where company0_.cmp_id=?
Hibernate: select company0_.id as id1_1_0_, company0_.cmp_id as cmp_id2_1_0_, company0_.company_name as company_3_1_0_ from company company0_ where company0_.cmp_id=?
Set<Employee> ee = cmp.getEmployee();
for(Employee e : ee){
System.out.println(e.getEmpName());
}