У меня есть три сущности Country
, State
, City
.
@Data
@Entity
@Table(name = "country", uniqueConstraints = { @UniqueConstraint(columnNames = { "name" }) })
public class Country {
@Id
@Column(name = "country_id")
private int countryId;
@Column(name = "name", length = 45)
private String name;
@Column(name = "phonecode", length = 45)
private int phoneCode;
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "country_id")
private Set<State> states;
}
@Data
@Entity
@Table(name = "state")
public class State {
@Id
@Column(name = "state_id")
private int stateId;
@Column(name = "name", length = 45)
@NotBlank
private String name;
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "state_id")
private Set<City> cities;
}
@Data
@Entity
@Table(name = "city")
public class City {
@Id
@Column(name = "city_id")
private int cityId;
@Column(name = "name", length = 45)
private String name;
}
Это соотношение между ними:
Country
1: N State
State
1: N City
Я использую Spring Framework, поэтому для моего DAO я использую JpaRepository
для CRUD
public interface CountryDao extends JpaRepository<Country, Integer>{}
Это тестовый пример, в котором я пытаюсьполучить все города страны, имеющие Id = 101
@Autowired
private CountryDao countryDao;
@Test
@Transactional
public void getAllStateWithCountryName() {
Country country = countryDao.findById(101).orElseThrow(() -> new EntityNotFoundException());
System.out.println("country: " + country.getName());
country.getStates().forEach((state) -> System.out.println("state: " + state.getName()));
}
. Тест дает мне данные о городах, но я вижу некоторые нежелательные операторы select
, сгенерированные для объекта City
.Я очень удивлен, увидев это, поскольку я использовал FetchType=LAZY
в OneToMany
отношении State
и City
, и я никогда не звонил state.getCities()
.
Есть идеи, в чем здесь проблема?
Это вывод:
Hibernate: select country0_.country_id as country_1_12_0_, country0_.name as name2_12_0_, country0_.phonecode as phonecod3_12_0_ from country country0_ where country0_.country_id=?
country: India
Hibernate: select states0_.country_id as country_3_38_0_, states0_.state_id as state_id1_38_0_, states0_.state_id as state_id1_38_1_, states0_.name as name2_38_1_ from state states0_ where states0_.country_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
state: Punjab
state: Lakshadweep
state: Kerala
state: Maharashtra
state: Himachal Pradesh
state: Madhya Pradesh
state: Natwar
state: Tamil Nadu
state: Gujarat
state: Uttarakhand
state: Delhi
state: Pondicherry
state: Haryana
state: Jammu and Kashmir
state: Rajasthan
state: Chandigarh
state: Sikkim
state: Arunachal Pradesh
state: Vaishali
state: Kenmore
state: Andhra Pradesh
state: Daman and Diu
state: Paschim Medinipur
state: Tripura
state: Chhattisgarh
state: Uttar Pradesh
state: Jharkhand
state: Manipur
state: Meghalaya
state: Andaman and Nicobar Islands
state: Mizoram
state: Goa
state: Karnataka
state: Dadra and Nagar Haveli
state: Odisha
state: West Bengal
state: Bihar
state: Assam
state: Narora
state: Nagaland
state: Telangana