I have this entity:
@Entity
public class PlantArea {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_plantarea")
@SequenceGenerator(name = "seq_plantarea", allocationSize = 1)
@Column(nullable = false, updatable = false)
private Long id;
private String code;
private String name;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="plantid", nullable = false)
private Plant plant;
}
In the repo I get all plantareas like this:
@Override
public List<PlantArea> getPlantAreas() {
return plantAreaRepository.findAll();
}
So, nothing special.
I have two question to this:
Hibernate fires two queries: one for selecting the plantareas and one for selecting the related plants. How can I avoid this and how can I force hibernate to do only one query? Like in sql (pseudo): select all plantareas inner join plants. I suppose I have to write some jpql/hql, whatever query?
Querying the plantareas with the simple
findAll()
gives this json result:[ { "id": 1, "code": "122", "name": "auto", "plant": { "id": 1, "code": "130", "location": "some city", "company": { "id": 1, "name": "test company long name", "shortName": "test company " } } } ]
As you can see, every plant has a company too. So, I get the full chain of the related objects: plantaraea -> plant -> company. The question is, how can I suppress the company
object? Basically I need a query like this (pseudo sql):
select plantarea.*, plant.*, company.* from plantarea
inner join plant on plantarea.plant_id=plant.id
inner join company on plant.company_id=company.id