I'm having trouble making OrderEntity object load BillingAddress lazily. I've seen questions revolving around this asked a lot and followed instructions, including adding optional = false but BillingAddress still gets loaded whenever I findById an OrderEntity.
These are my entities (reduced for the sake of this question):
OrderEntity
@Entity
@Table(name = "orders", schema = "glamitoms")
public class OrderEntity {
@Id
@Column(name = "id")
private int id;
@OneToOne(mappedBy = "order", cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = false)
private BillingAddressEntity billingAddress;
}
BillingAddressEntity
@Entity
@Table(name = "billing_address", schema = "glamitoms")
public class BillingAddressEntity {
@Id
@Column(name = "order_id")
private int id;
@OneToOne(fetch = FetchType.LAZY)
@MapsId
private OrderEntity order;
}
TestController
@RestController
public class TestController {
private OrdersDAO ordersDAO;
@Autowired
public TestController(OrdersDAO ordersDAO) {
this.ordersDAO = ordersDAO;
}
@GetMapping("/test")
public void test() {
OrderEntity orderEntity = ordersDAO.findById(1).get();
}
}
OrdersDAO
@Repository
public interface OrdersDAO extends JpaRepository<OrderEntity, Integer> {
}
The table billing_address has a FK referencing orders. I've read contradicting answers saying adding optional = false should load entities lazily, but for me, that doesn't seem to work. Am I missing something in any of these entities?