I am using Spring Data + Hibernate and have an entity with several @ManyToOne fields. Every field is retrieved by hibernate with separate select query.
I've already tried @Fetch(value = FetchMode.JOIN), but no use,
Example:
@Entity
public class MyEntity{
@Id
private Long id;
@JoinColumn(name = "A")
@Fetch(value = FetchMode.JOIN)
@ManyToOne
private A a;
@ManyToOne
@Fetch(value = FetchMode.JOIN)
@JoinColumn(name = "B")
private B b;
}
Hibernate will perform 3 db trips for MyEntity, A and B.
Is there a way to retrieve my entity and all of its fields with single select where A and B will be joined.?
Edit:
Entities A and B defined like this:
@Entity
public class A{
@Id
Long id;
String name;
}
Repository looks like this (Spring Data creates implementation):
public interface MyEntityRepository extends CrudRepository<MyEntity , Long> {}
And code that loads entity is pretty simple too:
myEntityRepository.findAll();
Edit 2:
I've just written QueryDSL query and for it only 1 select was executed. I'm happy with that at least.