0

A is associated with B in a one-on-one relation, the purpose is to retrieve columns from both A and B. First implementation took advantage of Hibernate Entity and relevant APIs, and the relationship was established via criteriaBuilder APIs. The problem was that the performance seemed suffered.

A proposed solution is to use a stored proc which basically a plain select a.*, b.* from A a join B b on ..... On the hibernate part, it calls the SP like:

Query q = session.getNamedQuery("sp_name")

B in A is like:

class A{
  @OneToOne(fetch = FetchType.LAZY)
  private B b;
...
}

the execution seemed failed to load values of B's properties.

So what did I miss? How can I make it work and at the same time, improve the performance?

J.E.Y
  • 1,173
  • 2
  • 15
  • 37
  • Have you tried using `JOIN FETCH` to get A and B in the same query in your original solution? See, for example, https://stackoverflow.com/questions/17431312/what-is-the-difference-between-join-and-join-fetch-when-using-jpa-and-hibernate – kidney Feb 14 '22 at 07:23
  • Thanks @Kidney, however my database is Oracle, I tried to use "FETCH" in the joined table SQL, it's my understanding that "FETCH" doesn't work with ORACLE SQLs. – J.E.Y Feb 14 '22 at 17:06
  • I meant `JOIN FETCH` in JPA, not SQL. You can either do it in JPQL, or you should be able to do it via the Criteria API as well. For example [here](https://thorben-janssen.com/hibernate-tips-difference-join-left-join-fetch-join/) and [here](https://stackoverflow.com/questions/50553489/how-to-do-a-join-fetch-in-jpa-criteria) – kidney Feb 15 '22 at 06:39

0 Answers0