Note that there are some explanatory texts on larger screens.

plurals
  1. POAdditional queries in JPA
    primarykey
    data
    text
    <p>I have two classes <code>InvitedPerson</code> and <code>Flight</code> with a one to one relationship with each other. Here is how they are annotated.</p> <pre><code>public class InvitedTech{ ... @OneToOne(mappedBy="invitedTech", cascade = CascadeType.ALL, fetch=FetchType.LAZY) public Flight flight; @OneToOne(mappedBy="invitedTech", cascade = CascadeType.ALL, fetch=FetchType.LAZY) public Hotel hotel; ... } public class Flight{ ... @OneToOne @JoinColumn(name="invitedTechId", nullable=false) public InvitedTech invitedTech; ... } </code></pre> <p>As you can see <code>Flight</code> is the owner of the relationship and <code>InvitedTech</code> is the other side of this bidirectional relationship. InvitedTech also has a <code>OneToOne</code> relationship with <code>Hotel</code> Now, when I write a simple query to fetch all flights, it triggers three queries in total. 1st which gets me the results, but fires 2 additional queries.</p> <pre><code>List&lt;Flight&gt; flg = JPA.em().createQuery("SELECT flg from Flight flg").getResultList(); </code></pre> <ol> <li>Query that gets all flights (This is the only one that I need)</li> <li>Query with a join between InvitedTech and Flight</li> <li>Query with a join between invitedTech and Hotel</li> </ol> <p>Why are query 2&amp;3 being executed even though I have set FetchType=Lazy. I am not accessing Hotel Information. And Flight should not be queries again as the first query returns the data. </p> <p>After some playing around when I remove <code>mappedBy</code> attribute from both the annotations, those 2 addition queries don't get executed(i.e only 1st gets executed). </p> <p>Why does the <code>mappedBy</code> attribute cause additional queries to be executed even though <code>FetchType=Lazy</code>. Is there a way to stop this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload