I have just started using Hibernate in my application. Also I'm using JPA repository for using custom query. I have multiple entities which have relationships between them. I have used Fetch mode as 'EAGER' on the join column and also using Cache. I observed that Hibernate is making multiple select queries to fetch each entity and not doing it in a single query.
Below is an entity:
@Entity
@Table(name = "entity_a")
public class EntityA implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
...
@OneToMany(fetch = FetchType.EAGER, mappedBy = "entity_a")
@JsonIgnore
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<EntityB> entityB = new HashSet<>();
I'm fetching my entity as below, using JPA custom queries:
EntityA entityA = entityARepository.findOneById(id);
Hibernate issues a select query for the above statement.
Whenever I do a entityA.getEntityB()
it issues another select statement on EntityB. How to avoid this. How can I get both entities in a single select using Hibernate?