我已经编写了用于从数据库获取数据的代码,但是它还会引发意外的查询:
@SuppressWarnings("unchecked")
@Transactional
public List<Job> getAppliedPositionsById(Long userId) {
// String currentDate = SQLDateFormator.getCurrentDateInSqlFormat();
String strQuery = "from Job x left join x.applications a where a.applicant.id = :userId";
Query query = entityManager.createQuery(strQuery);
query.setParameter("userId", userId);
return query.getResultList();
}
在
return query.getResultList();
上触发两个查询。由于第二个查询,我得到了例外。两个查询
休眠:
select
job0_.id as id1_5_0_,
applicatio1_.id as id1_1_1_,
job0_.close_date as close_da2_5_0_,
job0_.committee_chair_id as committe6_5_0_,
job0_.description as descript3_5_0_,
job0_.publish_date as publish_4_5_0_,
job0_.title as title5_5_0_,
applicatio1_.applicant_id as applican6_1_1_,
applicatio1_.current_job_institution as current_2_1_1_,
applicatio1_.current_job_title as current_3_1_1_,
applicatio1_.current_job_year as current_4_1_1_,
applicatio1_.cv_id as cv_id7_1_1_,
applicatio1_.job_id as job_id8_1_1_,
applicatio1_.research_statement_id as research9_1_1_,
applicatio1_.submit_date as submit_d5_1_1_,
applicatio1_.teaching_statement_id as teachin10_1_1_
from
jobs job0_
left outer join
applications applicatio1_
on job0_.id=applicatio1_.job_id
where
applicatio1_.applicant_id=?
休眠:
select
user0_.id as id1_8_0_,
user0_.address as address2_8_0_,
user0_.email as email3_8_0_,
user0_.first_name as first_na4_8_0_,
user0_.last_name as last_nam5_8_0_,
user0_.password as password6_8_0_,
user0_.phone as phone7_8_0_
from
users user0_
where
user0_.id=?
Users
表上的第二个查询完全没有必要。Job
实体 @Id
@GeneratedValue
private Long id;
private String title;
private String description;
@Column(name = "publish_date")
private Date publishDate;
@Column(name = "close_date")
private Date closeDate;
@ManyToOne
@JoinColumn(name = "committee_chair_id")
private User committeeChair;
@ManyToMany
@JoinTable(name = "job_committee_members",
joinColumns = @JoinColumn(name = "job_id") ,
inverseJoinColumns = @JoinColumn(name = "user_id") )
@OrderBy("lastName asc")
private List<User> committeeMembers;
@OneToMany(mappedBy = "job")
@OrderBy("date asc")
private List<Application> applications;
}
Application
实体: @Id
@GeneratedValue
private Long id;
@ManyToOne
private Job job;
@ManyToOne
private User applicant;
@Column(name = "submit_date")
private Date submitDate;
@Column(name = "current_job_title")
private String currentJobTitle;
@Column(name = "current_job_institution")
private String currentJobInstitution;
@Column(name = "current_job_year")
private Integer currentJobYear;
@ElementCollection
@CollectionTable(name = "application_degrees",
joinColumns = @JoinColumn(name = "application_id") )
@OrderBy("year desc")
private List<Degree> degrees;
@OneToOne
private File cv;
@OneToOne
@JoinColumn(name = "research_statement_id")
private File researchStatement;
@OneToOne
@JoinColumn(name = "teaching_statement_id")
private File teachingStatement;
@OneToMany(mappedBy = "application",
cascade = { CascadeType.MERGE, CascadeType.PERSIST })
@OrderColumn(name = "round_index")
private List<Round> rounds;
}
User
实体:@Id
@GeneratedValue
private Long id;
@Column(unique = true, nullable = false)
private String email;
@Column(nullable = false)
private String password;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
private String address;
private String phone;
@OneToMany(mappedBy = "applicant")
@OrderBy("id desc")
private List<Application> applications;
}
最佳答案
根据JPA 2.0规范,默认值如下所示:
一对多:LAZY
ManyToOne:EAGER
多对多:LAZY
一对一:EAGER
您在Application类中
@ManyToOne
private User applicant;
如果您将其切换为LAZY
@ManyToOne(fetch = FetchType.LAZY)
它应该按照您想要的方式工作。