Ole Ole - 18 days ago 6
Java Question

When using @OnetoOne(optional=false) does the target reference always get loaded?

In this example from this understanding-jpa-part-2-relationships-the-jpa-way tutorial will an

Order
entity always be loaded when an
Invoice
entity is loaded? Putting a code snippet from the
Invoice
class below for reference:

@OneToOne(optional=false)
@JoinColumn(name = "ORDER_ID")
private Order order;

Answer

Yes. Because if you don't specify the attribute fetch the default FetchType is EAGER.

If you want it loaded LAZY

@OneToOne(optional=false, fetch=FetchType.LAZY)
@JoinColumn(name = "ORDER_ID") 
private Order order;