I have some difficulties with Spring and Hibernate and lazy loading. There are a lot of questions and answers, but not really what i am looking for.
So lets say i have a Car class. With an id, name and one to many relationships with doors, windows and wheels. As they are oneToMany, they are default lazy loaded which is preferred because when i want to view the name of the car i dont want to see the tires and stuff. This works in my case, using the default findOne() methods of my repositories.
But when i want to view the tire pressure, i need to initialize the tires relationship. I used to do that with Hibernate.initialize(car.getTires()). This produces another SELECT query for the database. Now I want to improve my database queries and select only the car with the tires but leave out the windows and doors. (Which is possible in MySQL with joins). The option to make them load eager is out of question because i dont always want to load the tires relationship (I have some big relationships on my objects).
I tried the following approach:
@Query("SELECT c FROM Car c JOIN FETCH c.tires WHERE c.id = :id")
Car findOneWithTiresLoaded(@Param("id") Long id);
This does provide the right data. But after analyzing the object returned from the repository, i noticed all of the relationships are loaded. So this does not only return the car with the tires relationship, but also the doors and windows.
The following gives me the same output (with the moneToMany relationship loaded)
@Query("SELECT c FROM Car c WHERE id = :id")
Car findOneWithTiresLoaded(@Param("id") Long id);
Something that is not wanted. I expected this to output only the Car object without all its lazy relationships.
Also suggested by people on the internet is to call Car.getTires().size(). Which will also produce another SELECT query.
Is there any way to just select the Car with only the Tires relation ship loaded? Without the fetch =  FetchType.LAZY, Hibernate.initialize() or size() method? How is it not possible to just join one table? Also, i do not use XML for any configuration.
Thanks!
 
     
    