我正在执行这段代码:
currentExamTemplateId = 7l;
currentStudentId = 806l;
TemplateStudent ts = templateStudentRepository.findByTemplateAndStudent(currentExamTemplateId, currentStudentId);
currentExamTemplateId = 11l;
currentStudentId = 555l;
ts = templateStudentRepository.findByTemplateAndStudent(currentExamTemplateId, currentStudentId);
存储库查询以这种方式查找:
@Query("select a from TemplateStudent a where a.examTemplate.examTemplateId=:examTemplateId and a.student.studentId=:studentId")
TemplateStudent findByTemplateAndStudent(@Param("examTemplateId") Long examTemplateId, @Param("studentId") Long studentId);
执行第一个findByTemplateAndStudent调用时,没有什么奇怪的事情,SQL Server提供正确的数据,SQL语句看起来很正常:
2015-11-11 16:09:37 [http-apr-8080-exec-1] DEBUG
o.s.o.j.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler - Creating new EntityManager for shared EntityManager invocation
Hibernate:
select
templatest0_.templateStudentId as template1_16_,
templatest0_.examtemplateid as examtemp2_16_,
templatest0_.studentid as studenti3_16_
from
TemplateStudent templatest0_
where
templatest0_.examtemplateid=?
and templatest0_.studentid=?
2015-11-11 16:09:37 [http-apr-8080-exec-1] DEBUG
o.s.j.d.DriverManagerDataSource - Creating new JDBC DriverManager Connection to [jdbc:postgresql://localhost:5432/livekorrektur_dev]
2015-11-11 16:09:37 [http-apr-8080-exec-1] DEBUG
o.s.o.jpa.EntityManagerFactoryUtils - Closing JPA EntityManager
但是在执行第二个findByTemplateAndStudent之后,Hibernate发送了两个SQL查询。第一个看起来也很正常。
2015-11-11 16:09:57 [http-apr-8080-exec-1] DEBUG
o.s.o.j.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler - Creating new EntityManager for shared EntityManager invocation
Hibernate:
select
templatest0_.templateStudentId as template1_16_,
templatest0_.examtemplateid as examtemp2_16_,
templatest0_.studentid as studenti3_16_
from
TemplateStudent templatest0_
where
templatest0_.examtemplateid=?
and templatest0_.studentid=?
2015-11-11 16:09:57 [http-apr-8080-exec-1] DEBUG
o.s.j.d.DriverManagerDataSource - Creating new JDBC DriverManager Connection to [jdbc:postgresql://localhost:5432/livekorrektur_dev]
Hibernate:
select
examtempla0_.examTemplateId as examTemp1_6_0_,
examtempla0_.examId as examId3_6_0_,
examtempla0_.name as name2_6_0_,
exam1_.examId as examId1_4_1_,
exam1_.correctionEnd as correcti2_4_1_,
exam1_.correctionStart as correcti3_4_1_,
exam1_.created as created4_4_1_,
exam1_.gradeLevelId as gradeLe11_4_1_,
exam1_.name as name5_4_1_,
exam1_.semester as semester6_4_1_,
exam1_.shortName as shortNam7_4_1_,
exam1_.state as state8_4_1_,
exam1_.studentInfo as studentI9_4_1_,
exam1_.tightRange as tightRa10_4_1_,
correctors2_.examId as examId3_4_2_,
actor3_.actorId as actorId2_3_2_,
actor3_.actorId as actorId1_0_3_,
actor3_.created as created2_0_3_,
actor3_.email as email3_0_3_,
actor3_.enabled as enabled4_0_3_,
actor3_.firstName as firstNam5_0_3_,
actor3_.lastLogin as lastLogi6_0_3_,
actor3_.lastName as lastName7_0_3_,
actor3_.login as login8_0_3_,
actor3_.password as password9_0_3_,
roles4_.actorId as actorId2_0_4_,
role5_.roleId as roleId3_1_4_,
role5_.roleId as roleId1_11_5_,
role5_.description as descript2_11_5_,
role5_.displayName as displayN3_11_5_,
role5_.name as name4_11_5_,
exercises6_.examId as examId4_4_6_,
exercises6_.exerciseId as exercise1_7_6_,
exercises6_.exerciseId as exercise1_7_7_,
exercises6_.examId as examId4_7_7_,
exercises6_.maxPoints as maxPoint2_7_7_,
exercises6_.name as name3_7_7_,
exercises6_.parentExerciseId as parentEx5_7_7_,
exercise7_.exerciseId as exercise1_7_8_,
exercise7_.examId as examId4_7_8_,
exercise7_.maxPoints as maxPoint2_7_8_,
exercise7_.name as name3_7_8_,
exercise7_.parentExerciseId as parentEx5_7_8_,
gradelevel8_.gradeLevelId as gradeLev1_8_9_,
gradelevel8_.fourPointZero as fourPoin2_8_9_,
gradelevel8_.onePointSeven as onePoint3_8_9_,
gradelevel8_.onePointThree as onePoint4_8_9_,
gradelevel8_.onePointZero as onePoint5_8_9_,
gradelevel8_.threePointSeven as threePoi6_8_9_,
gradelevel8_.threePointThree as threePoi7_8_9_,
gradelevel8_.threePointZero as threePoi8_8_9_,
gradelevel8_.twoPointSeven as twoPoint9_8_9_,
gradelevel8_.twoPointThree as twoPoin10_8_9_,
gradelevel8_.twoPointZero as twoPoin11_8_9_,
participan9_.examId as examId2_4_10_,
student10_.studentId as studentI4_5_10_,
student10_.studentId as studentI1_14_11_,
student10_.enrolmentNumber as enrolmen2_14_11_,
student10_.firstName as firstNam3_14_11_,
student10_.lastName as lastName4_14_11_,
exercises11_.examTemplateId as examTemp2_6_12_,
exercise12_.exerciseId as exercise3_15_12_,
exercise12_.exerciseId as exercise1_7_13_,
exercise12_.examId as examId4_7_13_,
exercise12_.maxPoints as maxPoint2_7_13_,
exercise12_.name as name3_7_13_,
exercise12_.parentExerciseId as parentEx5_7_13_,
students13_.examTemplateId as examTemp2_6_14_,
student14_.studentId as studentI3_16_14_,
student14_.studentId as studentI1_14_15_,
student14_.enrolmentNumber as enrolmen2_14_15_,
student14_.firstName as firstNam3_14_15_,
student14_.lastName as lastName4_14_15_
from
ExamTemplate examtempla0_
left outer join
Exam exam1_
on examtempla0_.examId=exam1_.examId
left outer join
Corrector correctors2_
on exam1_.examId=correctors2_.examId
left outer join
Actor actor3_
on correctors2_.actorId=actor3_.actorId
left outer join
ActorRole roles4_
on actor3_.actorId=roles4_.actorId
left outer join
Role role5_
on roles4_.roleId=role5_.roleId
left outer join
Exercise exercises6_
on exam1_.examId=exercises6_.examId
left outer join
Exercise exercise7_
on exercises6_.parentExerciseId=exercise7_.exerciseId
left outer join
GradeLevel gradelevel8_
on exam1_.gradeLevelId=gradelevel8_.gradeLevelId
left outer join
ExamParticipant participan9_
on exam1_.examId=participan9_.examId
left outer join
Student student10_
on participan9_.studentId=student10_.studentId
left outer join
TemplateExercise exercises11_
on examtempla0_.examTemplateId=exercises11_.examTemplateId
left outer join
Exercise exercise12_
on exercises11_.exerciseId=exercise12_.exerciseId
left outer join
TemplateStudent students13_
on examtempla0_.examTemplateId=students13_.examTemplateId
left outer join
Student student14_
on students13_.studentId=student14_.studentId
where
examtempla0_.examTemplateId=?
order by
student10_.studentId
第二个语句使用了很多连接,我不知道为什么会调用这个语句。通过将此查询发送到PostgreSQL,服务器使用了大量的CPU / IO并且无法响应(到目前为止我已经等了5分钟)。
有没有人有这个问题引起的想法?