Issue
H2 database can work completely in-memory mode - data stored in RAM and not on discspace, no I/O operations, better performance etc(as I understand). Let's consider having next simple entity.
@Entity
@Table(name = "USER")
public class User {
@Id
@GeneratedValue
@Column(name = "USER_ID")
private Long userId;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
private Set<OrderDetail> orderDetail = new HashSet();
}
Using standart JpaRepository we can save it in memory and perform other CRUD operations. However, in order to retrieve already saved entity with dependencies on other entities(orderDetail for User), we still have to either eagerly load it with fetch graph, JPQL join statement, access to it in transaction or directly pointing collection fetch type. And things get even worse when we are facing multiple dependencies, which leads either to n+1 or cartesian product on eager fetching with entity graph.
In my understanding, since entities stored in-memory, why it doesn't allow to load entire entity graph with all nested relations in one call? Isn't this thing just about storing references on related entities?
Solution
why it doesn't allow to load entire entity graph with all nested relations in one call?
Because JPA communicates with any database using JDBC and doesn't care if the database stores the data in memory, on disc, flash drives or stone slabs. Also, just because an in-memory database doesn't store it's data on some persistent storage doesn't mean accessing data in it is free. You still have to construct objects from the data stored.
Of course, you can always store your objects directly in memory using for example a HashMap or similar data structures, without any conversion, basically just as a reference.
Answered By - Jens Schauder