Hibernate在单个查询中执行了太多的连接操作。

huangapple go评论72阅读模式
英文:

Hibernate does too much joins for a single query

问题

I have an airport database that I downloaded from the mysql site (I also uploaded a photo of it here) and I made all its settings in the spring boot program, but when I want to run a simple query select f from Flight, hibernate does a lot of strange joins and it doesn't end and the outOfMemory error is shown. In the following output, a part of hibernate queries are placed, which are not all of them, and in the middle of the java.lang.OutOfMemoryError: Java heap space is shown, and the program is terminated.

I had two questions:

First of all, what is the reason for all these joins?

Second, what is the way to prevent it?

All models classes(with hibernate mappings)-you dont need to see the view folder.

EER photo:

Hibernate在单个查询中执行了太多的连接操作。

output:

英文:

I have an airport database that I downloaded from the mysql site (I also uploaded a photo of it here) and I made all its settings in the spring boot program, but when I want to run a simple query select f from Flight, hibernate does a lot of strange joins and it doesn't end and the outOfMemory error is shown. In the following output, a part of hibernate queries are placed, which are not all of them, and in the middle of the java.lang.OutOfMemoryError: Java heap space is shown, and the program is terminated.

I had two questions:

First of all, what is the reason for all these joins?

Second, what is the way to prevent it?

All models classes(with hibernate mappings)-you dont need to see the view folder.

EER photo:

Hibernate在单个查询中执行了太多的连接操作。

output:

Hibernate: 
select
flight0_.flight_id as flight_i1_9_,
flight0_.airline_id as airline_4_9_,
flight0_.airplane_id as airplane5_9_,
flight0_.arrival as arrival2_9_,
flight0_.departure as departur3_9_,
flight0_.from as from6_9_,
flight0_.flightno as flightno7_9_,
flight0_.to as to8_9_ 
from
flight flight0_ 
order by
flight0_.departure asc limit ?
Hibernate: 
select
airline0_.airline_id as airline_1_0_0_,
airline0_.airlinename as airlinen2_0_0_,
airline0_.base_airport as base_air4_0_0_,
airline0_.iata as iata3_0_0_,
airport1_.airport_id as airport_1_3_1_,
airport1_.iata as iata2_3_1_,
airport1_.icao as icao3_3_1_,
airport1_.name as name4_3_1_,
airportgeo2_.airport_id as airport_1_4_2_,
airportgeo2_.airport_id as airport_1_4_3_,
airportgeo2_.city as city2_4_3_,
airportgeo2_.country as country3_4_3_,
airportgeo2_.geolocation as geolocat4_4_3_,
airportgeo2_.latitude as latitude5_4_3_,
airportgeo2_.longitude as longitud6_4_3_,
airportgeo2_.name as name7_4_3_,
airportrea3_.airport_id as airport_1_5_4_,
airportrea3_.airport_id as airport_1_5_5_,
airportrea3_.hops as hops2_5_5_ 
from
airline airline0_ 
left outer join
airport airport1_ 
on airline0_.base_airport=airport1_.airport_id 
left outer join
airport_geo airportgeo2_ 
on airport1_.airport_id=airportgeo2_.airport_id 
left outer join
airport_reachable airportrea3_ 
on airport1_.airport_id=airportrea3_.airport_id 
where
airline0_.airline_id=?
Hibernate: 
select
airplane0_.airplane_id as airplane1_1_0_,
airplane0_.airline_id as airline_2_1_0_,
airplane0_.type_id as type_id4_1_0_,
airplane0_.capacity as capacity3_1_0_,
airplanety1_.type_id as type_id1_2_1_,
airplanety1_.description as descript2_2_1_,
airplanety1_.identifier as identifi3_2_1_,
flights2_.airplane_id as airplane5_9_2_,
flights2_.flight_id as flight_i1_9_2_,
flights2_.flight_id as flight_i1_9_3_,
flights2_.airline_id as airline_4_9_3_,
flights2_.airplane_id as airplane5_9_3_,
flights2_.arrival as arrival2_9_3_,
flights2_.departure as departur3_9_3_,
flights2_.from as from6_9_3_,
flights2_.flightno as flightno7_9_3_,
flights2_.to as to8_9_3_,
airline3_.airline_id as airline_1_0_4_,
airline3_.airlinename as airlinen2_0_4_,
airline3_.base_airport as base_air4_0_4_,
airline3_.iata as iata3_0_4_,
bookings4_.flight_id as flight_i4_7_5_,
bookings4_.booking_id as booking_1_7_5_,
bookings4_.booking_id as booking_1_7_6_,
bookings4_.flight_id as flight_i4_7_6_,
bookings4_.passenger_id as passenge5_7_6_,
bookings4_.price as price2_7_6_,
bookings4_.seat as seat3_7_6_,
airport5_.airport_id as airport_1_3_7_,
airport5_.iata as iata2_3_7_,
airport5_.icao as icao3_3_7_,
airport5_.name as name4_3_7_,
logs6_.flight_id as flight_19_10_8_,
logs6_.flight_log_id as flight_l1_10_8_,
logs6_.flight_log_id as flight_l1_10_9_,
logs6_.airline_id_new as airline_2_10_9_,
logs6_.airline_id_old as airline_3_10_9_,
logs6_.airplane_id_new as airplane4_10_9_,
logs6_.airplane_id_old as airplane5_10_9_,
logs6_.arrival_new as arrival_6_10_9_,
logs6_.arrival_old as arrival_7_10_9_,
logs6_.comment as comment8_10_9_,
logs6_.departure_new as departur9_10_9_,
logs6_.departure_old as departu10_10_9_,
logs6_.flight_id as flight_19_10_9_,
logs6_.flightno_new as flightn11_10_9_,
logs6_.flightno_old as flightn12_10_9_,
logs6_.from_new as from_ne13_10_9_,
logs6_.from_old as from_ol14_10_9_,
logs6_.log_date as log_dat15_10_9_,
logs6_.to_new as to_new16_10_9_,
logs6_.to_old as to_old17_10_9_,
logs6_.user as user18_10_9_,
flightsche7_.flightno as flightno1_11_10_,
flightsche7_.airline_id as airline11_11_10_,
flightsche7_.arrival as arrival2_11_10_,
flightsche7_.departure as departur3_11_10_,
flightsche7_.friday as friday4_11_10_,
flightsche7_.from as from12_11_10_,
flightsche7_.monday as monday5_11_10_,
flightsche7_.saturday as saturday6_11_10_,
flightsche7_.sunday as sunday7_11_10_,
flightsche7_.thursday as thursday8_11_10_,
flightsche7_.to as to13_11_10_,
flightsche7_.tuesday as tuesday9_11_10_,
flightsche7_.wednesday as wednesd10_11_10_,
airport8_.airport_id as airport_1_3_11_,
airport8_.iata as iata2_3_11_,
airport8_.icao as icao3_3_11_,
airport8_.name as name4_3_11_ 
from
airplane airplane0_ 
left outer join
airplane_type airplanety1_ 
on airplane0_.type_id=airplanety1_.type_id 
left outer join
flight flights2_ 
on airplane0_.airplane_id=flights2_.airplane_id 
left outer join
airline airline3_ 
on flights2_.airline_id=airline3_.airline_id 
left outer join
booking bookings4_ 
on flights2_.flight_id=bookings4_.flight_id 
left outer join
airport airport5_ 
on flights2_.from=airport5_.airport_id 
left outer join
flight_log logs6_ 
on flights2_.flight_id=logs6_.flight_id 
left outer join
flightschedule flightsche7_ 
on flights2_.flightno=flightsche7_.flightno 
left outer join
airport airport8_ 
on flights2_.to=airport8_.airport_id 
where
airplane0_.airplane_id=?
Hibernate: 
select
airline0_.airline_id as airline_1_0_0_,
airline0_.airlinename as airlinen2_0_0_,
airline0_.base_airport as base_air4_0_0_,
airline0_.iata as iata3_0_0_,
airport1_.airport_id as airport_1_3_1_,
airport1_.iata as iata2_3_1_,
airport1_.icao as icao3_3_1_,
airport1_.name as name4_3_1_,
airportgeo2_.airport_id as airport_1_4_2_,
airportgeo2_.airport_id as airport_1_4_3_,
airportgeo2_.city as city2_4_3_,
airportgeo2_.country as country3_4_3_,
airportgeo2_.geolocation as geolocat4_4_3_,
airportgeo2_.latitude as latitude5_4_3_,
airportgeo2_.longitude as longitud6_4_3_,
airportgeo2_.name as name7_4_3_,
airportrea3_.airport_id as airport_1_5_4_,
airportrea3_.airport_id as airport_1_5_5_,
airportrea3_.hops as hops2_5_5_ 
from
airline airline0_ 
left outer join
airport airport1_ 
on airline0_.base_airport=airport1_.airport_id 
left outer join
airport_geo airportgeo2_ 
on airport1_.airport_id=airportgeo2_.airport_id 
left outer join
airport_reachable airportrea3_ 
on airport1_.airport_id=airportrea3_.airport_id 
where
airline0_.airline_id=?
Hibernate: 
select
airplane0_.airplane_id as airplane1_1_0_,
airplane0_.airline_id as airline_2_1_0_,
airplane0_.type_id as type_id4_1_0_,
airplane0_.capacity as capacity3_1_0_,
airplanety1_.type_id as type_id1_2_1_,
airplanety1_.description as descript2_2_1_,
airplanety1_.identifier as identifi3_2_1_,
flights2_.airplane_id as airplane5_9_2_,
flights2_.flight_id as flight_i1_9_2_,
flights2_.flight_id as flight_i1_9_3_,
flights2_.airline_id as airline_4_9_3_,
flights2_.airplane_id as airplane5_9_3_,
flights2_.arrival as arrival2_9_3_,
flights2_.departure as departur3_9_3_,
flights2_.from as from6_9_3_,
flights2_.flightno as flightno7_9_3_,
flights2_.to as to8_9_3_,
airline3_.airline_id as airline_1_0_4_,
airline3_.airlinename as airlinen2_0_4_,
airline3_.base_airport as base_air4_0_4_,
airline3_.iata as iata3_0_4_,
bookings4_.flight_id as flight_i4_7_5_,
bookings4_.booking_id as booking_1_7_5_,
bookings4_.booking_id as booking_1_7_6_,
bookings4_.flight_id as flight_i4_7_6_,
bookings4_.passenger_id as passenge5_7_6_,
bookings4_.price as price2_7_6_,
bookings4_.seat as seat3_7_6_,
airport5_.airport_id as airport_1_3_7_,
airport5_.iata as iata2_3_7_,
airport5_.icao as icao3_3_7_,
airport5_.name as name4_3_7_,
logs6_.flight_id as flight_19_10_8_,
logs6_.flight_log_id as flight_l1_10_8_,
logs6_.flight_log_id as flight_l1_10_9_,
logs6_.airline_id_new as airline_2_10_9_,
logs6_.airline_id_old as airline_3_10_9_,
logs6_.airplane_id_new as airplane4_10_9_,
logs6_.airplane_id_old as airplane5_10_9_,
logs6_.arrival_new as arrival_6_10_9_,
logs6_.arrival_old as arrival_7_10_9_,
logs6_.comment as comment8_10_9_,
logs6_.departure_new as departur9_10_9_,
logs6_.departure_old as departu10_10_9_,
logs6_.flight_id as flight_19_10_9_,
logs6_.flightno_new as flightn11_10_9_,
logs6_.flightno_old as flightn12_10_9_,
logs6_.from_new as from_ne13_10_9_,
logs6_.from_old as from_ol14_10_9_,
logs6_.log_date as log_dat15_10_9_,
logs6_.to_new as to_new16_10_9_,
logs6_.to_old as to_old17_10_9_,
logs6_.user as user18_10_9_,
flightsche7_.flightno as flightno1_11_10_,
flightsche7_.airline_id as airline11_11_10_,
flightsche7_.arrival as arrival2_11_10_,
flightsche7_.departure as departur3_11_10_,
flightsche7_.friday as friday4_11_10_,
flightsche7_.from as from12_11_10_,
flightsche7_.monday as monday5_11_10_,
flightsche7_.saturday as saturday6_11_10_,
flightsche7_.sunday as sunday7_11_10_,
flightsche7_.thursday as thursday8_11_10_,
flightsche7_.to as to13_11_10_,
flightsche7_.tuesday as tuesday9_11_10_,
flightsche7_.wednesday as wednesd10_11_10_,
airport8_.airport_id as airport_1_3_11_,
airport8_.iata as iata2_3_11_,
airport8_.icao as icao3_3_11_,
airport8_.name as name4_3_11_ 
from
airplane airplane0_ 
left outer join
airplane_type airplanety1_ 
on airplane0_.type_id=airplanety1_.type_id 
left outer join
flight flights2_ 
on airplane0_.airplane_id=flights2_.airplane_id 
left outer join
airline airline3_ 
on flights2_.airline_id=airline3_.airline_id 
left outer join
booking bookings4_ 
on flights2_.flight_id=bookings4_.flight_id 
left outer join
airport airport5_ 
on flights2_.from=airport5_.airport_id 
left outer join
flight_log logs6_ 
on flights2_.flight_id=logs6_.flight_id 
left outer join
flightschedule flightsche7_ 
on flights2_.flightno=flightsche7_.flightno 
left outer join
airport airport8_ 
on flights2_.to=airport8_.airport_id 
where
airplane0_.airplane_id=?
Hibernate: 
select
passenger0_.passenger_id as passenge1_13_0_,
passenger0_.firstname as firstnam2_13_0_,
passenger0_.lastname as lastname3_13_0_,
passenger0_.passportno as passport4_13_0_,
bookings1_.passenger_id as passenge5_7_1_,
bookings1_.booking_id as booking_1_7_1_,
bookings1_.booking_id as booking_1_7_2_,
bookings1_.flight_id as flight_i4_7_2_,
bookings1_.passenger_id as passenge5_7_2_,
bookings1_.price as price2_7_2_,
bookings1_.seat as seat3_7_2_,
flight2_.flight_id as flight_i1_9_3_,
flight2_.airline_id as airline_4_9_3_,
flight2_.airplane_id as airplane5_9_3_,
flight2_.arrival as arrival2_9_3_,
flight2_.departure as departur3_9_3_,
flight2_.from as from6_9_3_,
flight2_.flightno as flightno7_9_3_,
flight2_.to as to8_9_3_,
passengerd3_.passenger_id as passenge1_14_4_,
passengerd3_.passenger_id as passenge1_14_5_,
passengerd3_.birthdate as birthdat2_14_5_,
passengerd3_.city as city3_14_5_,
passengerd3_.country as country4_14_5_,
passengerd3_.emailaddress as emailadd5_14_5_,
passengerd3_.gender as gender6_14_5_,
passengerd3_.street as street7_14_5_,
passengerd3_.telephoneno as telephon8_14_5_,
passengerd3_.zip as zip9_14_5_ 
from
passenger passenger0_ 
left outer join
booking bookings1_ 
on passenger0_.passenger_id=bookings1_.passenger_id 
left outer join
flight flight2_ 
on bookings1_.flight_id=flight2_.flight_id 
left outer join
passengerdetails passengerd3_ 
on passenger0_.passenger_id=passengerd3_.passenger_id 
where
passenger0_.passenger_id=?
Hibernate: 
select
airline0_.airline_id as airline_1_0_0_,
airline0_.airlinename as airlinen2_0_0_,
airline0_.base_airport as base_air4_0_0_,
airline0_.iata as iata3_0_0_,
airport1_.airport_id as airport_1_3_1_,
airport1_.iata as iata2_3_1_,
airport1_.icao as icao3_3_1_,
airport1_.name as name4_3_1_,
airportgeo2_.airport_id as airport_1_4_2_,
airportgeo2_.airport_id as airport_1_4_3_,
airportgeo2_.city as city2_4_3_,
airportgeo2_.country as country3_4_3_,
airportgeo2_.geolocation as geolocat4_4_3_,
airportgeo2_.latitude as latitude5_4_3_,
airportgeo2_.longitude as longitud6_4_3_,
airportgeo2_.name as name7_4_3_,
airportrea3_.airport_id as airport_1_5_4_,
airportrea3_.airport_id as airport_1_5_5_,
airportrea3_.hops as hops2_5_5_ 
from
airline airline0_ 
left outer join
airport airport1_ 
on airline0_.base_airport=airport1_.airport_id 
left outer join
airport_geo airportgeo2_ 
on airport1_.airport_id=airportgeo2_.airport_id 
left outer join
airport_reachable airportrea3_ 
on airport1_.airport_id=airportrea3_.airport_id 
where
airline0_.airline_id=?
Hibernate: 
select
airplane0_.airplane_id as airplane1_1_0_,
airplane0_.airline_id as airline_2_1_0_,
airplane0_.type_id as type_id4_1_0_,
airplane0_.capacity as capacity3_1_0_,
airplanety1_.type_id as type_id1_2_1_,
airplanety1_.description as descript2_2_1_,
airplanety1_.identifier as identifi3_2_1_,
flights2_.airplane_id as airplane5_9_2_,
flights2_.flight_id as flight_i1_9_2_,
flights2_.flight_id as flight_i1_9_3_,
flights2_.airline_id as airline_4_9_3_,
flights2_.airplane_id as airplane5_9_3_,
flights2_.arrival as arrival2_9_3_,
flights2_.departure as departur3_9_3_,
flights2_.from as from6_9_3_,
flights2_.flightno as flightno7_9_3_,
flights2_.to as to8_9_3_,
airline3_.airline_id as airline_1_0_4_,
airline3_.airlinename as airlinen2_0_4_,
airline3_.base_airport as base_air4_0_4_,
airline3_.iata as iata3_0_4_,
bookings4_.flight_id as flight_i4_7_5_,
bookings4_.booking_id as booking_1_7_5_,
bookings4_.booking_id as booking_1_7_6_,
bookings4_.flight_id as flight_i4_7_6_,
bookings4_.passenger_id as passenge5_7_6_,
bookings4_.price as price2_7_6_,
bookings4_.seat as seat3_7_6_,
airport5_.airport_id as airport_1_3_7_,
airport5_.iata as iata2_3_7_,
airport5_.icao as icao3_3_7_,
airport5_.name as name4_3_7_,
logs6_.flight_id as flight_19_10_8_,
logs6_.flight_log_id as flight_l1_10_8_,
logs6_.flight_log_id as flight_l1_10_9_,
logs6_.airline_id_new as airline_2_10_9_,
logs6_.airline_id_old as airline_3_10_9_,
logs6_.airplane_id_new as airplane4_10_9_,
logs6_.airplane_id_old as airplane5_10_9_,
logs6_.arrival_new as arrival_6_10_9_,
logs6_.arrival_old as arrival_7_10_9_,
logs6_.comment as comment8_10_9_,
logs6_.departure_new as departur9_10_9_,
logs6_.departure_old as departu10_10_9_,
logs6_.flight_id as flight_19_10_9_,
logs6_.flightno_new as flightn11_10_9_,
logs6_.flightno_old as flightn12_10_9_,
logs6_.from_new as from_ne13_10_9_,
logs6_.from_old as from_ol14_10_9_,
logs6_.log_date as log_dat15_10_9_,
logs6_.to_new as to_new16_10_9_,
logs6_.to_old as to_old17_10_9_,
logs6_.user as user18_10_9_,
flightsche7_.flightno as flightno1_11_10_,
flightsche7_.airline_id as airline11_11_10_,
flightsche7_.arrival as arrival2_11_10_,
flightsche7_.departure as departur3_11_10_,
flightsche7_.friday as friday4_11_10_,
flightsche7_.from as from12_11_10_,
flightsche7_.monday as monday5_11_10_,
flightsche7_.saturday as saturday6_11_10_,
flightsche7_.sunday as sunday7_11_10_,
flightsche7_.thursday as thursday8_11_10_,
flightsche7_.to as to13_11_10_,
flightsche7_.tuesday as tuesday9_11_10_,
flightsche7_.wednesday as wednesd10_11_10_,
airport8_.airport_id as airport_1_3_11_,
airport8_.iata as iata2_3_11_,
airport8_.icao as icao3_3_11_,
airport8_.name as name4_3_11_ 
from
airplane airplane0_ 
left outer join
airplane_type airplanety1_ 
on airplane0_.type_id=airplanety1_.type_id 
left outer join
flight flights2_ 
on airplane0_.airplane_id=flights2_.airplane_id 
left outer join
airline airline3_ 
on flights2_.airline_id=airline3_.airline_id 
left outer join
booking bookings4_ 
on flights2_.flight_id=bookings4_.flight_id 
left outer join
airport airport5_ 
on flights2_.from=airport5_.airport_id 
left outer join
flight_log logs6_ 
on flights2_.flight_id=logs6_.flight_id 
left outer join
flightschedule flightsche7_ 
on flights2_.flightno=flightsche7_.flightno 
left outer join
airport airport8_ 
on flights2_.to=airport8_.airport_id 
where
airplane0_.airplane_id=?
Hibernate: 
select
passenger0_.passenger_id as passenge1_13_0_,
passenger0_.firstname as firstnam2_13_0_,
passenger0_.lastname as lastname3_13_0_,
passenger0_.passportno as passport4_13_0_,
bookings1_.passenger_id as passenge5_7_1_,
bookings1_.booking_id as booking_1_7_1_,
bookings1_.booking_id as booking_1_7_2_,
bookings1_.flight_id as flight_i4_7_2_,
bookings1_.passenger_id as passenge5_7_2_,
bookings1_.price as price2_7_2_,
bookings1_.seat as seat3_7_2_,
flight2_.flight_id as flight_i1_9_3_,
flight2_.airline_id as airline_4_9_3_,
flight2_.airplane_id as airplane5_9_3_,
flight2_.arrival as arrival2_9_3_,
flight2_.departure as departur3_9_3_,
flight2_.from as from6_9_3_,
flight2_.flightno as flightno7_9_3_,
flight2_.to as to8_9_3_,
passengerd3_.passenger_id as passenge1_14_4_,
passengerd3_.passenger_id as passenge1_14_5_,
passengerd3_.birthdate as birthdat2_14_5_,
passengerd3_.city as city3_14_5_,
passengerd3_.country as country4_14_5_,
passengerd3_.emailaddress as emailadd5_14_5_,
passengerd3_.gender as gender6_14_5_,
passengerd3_.street as street7_14_5_,
passengerd3_.telephoneno as telephon8_14_5_,
passengerd3_.zip as zip9_14_5_ 
from
passenger passenger0_ 
left outer join
booking bookings1_ 
on passenger0_.passenger_id=bookings1_.passenger_id 
left outer join
flight flight2_ 
on bookings1_.flight_id=flight2_.flight_id 
left outer join
passengerdetails passengerd3_ 
on passenger0_.passenger_id=passengerd3_.passenger_id 
where
passenger0_.passenger_id=?
Hibernate: 
select
airplane0_.airplane_id as airplane1_1_0_,
airplane0_.airline_id as airline_2_1_0_,
airplane0_.type_id as type_id4_1_0_,
airplane0_.capacity as capacity3_1_0_,
airplanety1_.type_id as type_id1_2_1_,
airplanety1_.description as descript2_2_1_,
airplanety1_.identifier as identifi3_2_1_,
flights2_.airplane_id as airplane5_9_2_,
flights2_.flight_id as flight_i1_9_2_,
flights2_.flight_id as flight_i1_9_3_,
flights2_.airline_id as airline_4_9_3_,
flights2_.airplane_id as airplane5_9_3_,
flights2_.arrival as arrival2_9_3_,
flights2_.departure as departur3_9_3_,
flights2_.from as from6_9_3_,
flights2_.flightno as flightno7_9_3_,
flights2_.to as to8_9_3_,
airline3_.airline_id as airline_1_0_4_,
airline3_.airlinename as airlinen2_0_4_,
airline3_.base_airport as base_air4_0_4_,
airline3_.iata as iata3_0_4_,
bookings4_.flight_id as flight_i4_7_5_,
bookings4_.booking_id as booking_1_7_5_,
bookings4_.booking_id as booking_1_7_6_,
bookings4_.flight_id as flight_i4_7_6_,
bookings4_.passenger_id as passenge5_7_6_,
bookings4_.price as price2_7_6_,
bookings4_.seat as seat3_7_6_,
airport5_.airport_id as airport_1_3_7_,
airport5_.iata as iata2_3_7_,
airport5_.icao as icao3_3_7_,
airport5_.name as name4_3_7_,
logs6_.flight_id as flight_19_10_8_,
logs6_.flight_log_id as flight_l1_10_8_,
logs6_.flight_log_id as flight_l1_10_9_,
logs6_.airline_id_new as airline_2_10_9_,
logs6_.airline_id_old as airline_3_10_9_,
logs6_.airplane_id_new as airplane4_10_9_,
logs6_.airplane_id_old as airplane5_10_9_,
logs6_.arrival_new as arrival_6_10_9_,
logs6_.arrival_old as arrival_7_10_9_,
logs6_.comment as comment8_10_9_,
logs6_.departure_new as departur9_10_9_,
logs6_.departure_old as departu10_10_9_,
logs6_.flight_id as flight_19_10_9_,
logs6_.flightno_new as flightn11_10_9_,
logs6_.flightno_old as flightn12_10_9_,
logs6_.from_new as from_ne13_10_9_,
logs6_.from_old as from_ol14_10_9_,
logs6_.log_date as log_dat15_10_9_,
logs6_.to_new as to_new16_10_9_,
logs6_.to_old as to_old17_10_9_,
logs6_.user as user18_10_9_,
flightsche7_.flightno as flightno1_11_10_,
flightsche7_.airline_id as airline11_11_10_,
flightsche7_.arrival as arrival2_11_10_,
flightsche7_.departure as departur3_11_10_,
flightsche7_.friday as friday4_11_10_,
flightsche7_.from as from12_11_10_,
flightsche7_.monday as monday5_11_10_,
flightsche7_.saturday as saturday6_11_10_,
flightsche7_.sunday as sunday7_11_10_,
flightsche7_.thursday as thursday8_11_10_,
flightsche7_.to as to13_11_10_,
flightsche7_.tuesday as tuesday9_11_10_,
flightsche7_.wednesday as wednesd10_11_10_,
airport8_.airport_id as airport_1_3_11_,
airport8_.iata as iata2_3_11_,
airport8_.icao as icao3_3_11_,
airport8_.name as name4_3_11_ 
from
airplane airplane0_ 
left outer join
airplane_type airplanety1_ 
on airplane0_.type_id=airplanety1_.type_id 
left outer join
flight flights2_ 
on airplane0_.airplane_id=flights2_.airplane_id 
left outer join
airline airline3_ 
on flights2_.airline_id=airline3_.airline_id 
left outer join
booking bookings4_ 
on flights2_.flight_id=bookings4_.flight_id 
left outer join
airport airport5_ 
on flights2_.from=airport5_.airport_id 
left outer join
flight_log logs6_ 
on flights2_.flight_id=logs6_.flight_id 
left outer join
flightschedule flightsche7_ 
on flights2_.flightno=flightsche7_.flightno 
left outer join
airport airport8_ 
on flights2_.to=airport8_.airport_id 
where
airplane0_.airplane_id=?

答案1

得分: 2

你在你的模型中只定义了EAGER关联,这些关联将始终进行连接和提取。
当关联是必需的时,应该设置EAGER提取类型,并尽量最大程度地使用LAZY关联。

https://www.baeldung.com/hibernate-lazy-eager-loading

public class Flight extends BaseEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "flight_id", insertable = false, updatable = false)
    private int flightId;
    @ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.REFRESH})
    @JoinColumn(name = "from")
    private Airport fromAirport;
    @ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.REFRESH})
    @JoinColumn(name = "to")
    private Airport toAirport;

    @ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.REFRESH})
    @JoinColumn(name = "flightno", insertable = false, updatable = false)
    private FlightSchedule schedule;
    @ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.REFRESH})
    @JoinColumn(name = "airline_id")
    private Airline airline;
    @ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.REFRESH})
    @JoinColumn(name = "airplane_id")
    private Airplane airplane;
    @OneToMany(mappedBy = "flight", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private Set<FlightLog> logs = new HashSet<>();
    @OneToMany(mappedBy = "flight", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private Set<Booking> bookings = new HashSet<>();
}

select f from Flight将提取Flight,Flight.Airport (EAGER),Flight.Airport.AirportGeo (EAGER),等等。

英文:

You defined in your model only EAGER relations, these relations will always be join and fetch.
You should set EAGER fetch type only when the relation is required and try to maximise LAZY relations.

https://www.baeldung.com/hibernate-lazy-eager-loading

public class Flight extends BaseEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = &quot;flight_id&quot;, insertable = false, updatable = false)
    private int flightId;
    @ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.REFRESH})
    @JoinColumn(name = &quot;from&quot;)
    private Airport fromAirport;
    @ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.REFRESH})
    @JoinColumn(name = &quot;to&quot;)
    private Airport toAirport;

    @ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.REFRESH})
    @JoinColumn(name = &quot;flightno&quot;, insertable = false, updatable = false)
    private FlightSchedule schedule;
    @ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.REFRESH})
    @JoinColumn(name = &quot;airline_id&quot;)
    private Airline airline;
    @ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.REFRESH})
    @JoinColumn(name = &quot;airplane_id&quot;)
    private Airplane airplane;
    @OneToMany(mappedBy = &quot;flight&quot;, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private Set&lt;FlightLog&gt; logs = new HashSet&lt;&gt;();
    @OneToMany(mappedBy = &quot;flight&quot;, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private Set&lt;Booking&gt; bookings = new HashSet&lt;&gt;();
}

select f from Flight will fetch Flight, Flight.Airport (EAGER), Flight.Airport.AirportGeo (EAGER), etc

huangapple
  • 本文由 发表于 2023年3月8日 15:34:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75670362.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定