JPA and Hibernate : one to one mapping causes three select queries -
jpa 2.0 hibernate 4.3.5
hi,
following onetoone mapping (with example code assuming 1 customer can have 1 order)
class customer { private order order; @onetoone(mappedby="customer", fetch=fetchtype.lazy) public order getorder() { return order; } public void setorder(order order) { this.order = order ; } } class order { private customer customer; @onetoone(fetch=fetchtype.lazy) @joincolumn(name="cust_id") public customer getcustomer() { return customer; } public void setcustomer(customer customer) { this.customer = customer; } } //calling code order order = em.find(order.class, 4); // line 1 system.out.println(order.getcustomer()); // line 2 </code>
above calling code resulting in 3 select statements i.e
line 1 causing
select * order id = ? #1
line 2 causing following 2 statements
select * customer id = ? #2 select * order cust_id = ? #3
my question: shouldn't 2 queries (i.e #1 , #2) given lazy fetching enabled on both ends ?
thanks, rakesh
you have defined both relationships lazy.
when load order customer attribute not loaded because lazy. lazy attribute loaded when accessed or if define eagerly loaded.
every mapping finished 1 (onetoone or manytoone) eagerly default, set lazy.
try define relationship eager:
@onetoone(fetch=fetchtype.eager) // or @onetoone @joincolumn(name="cust_id") public customer getcustomer() { return customer; }
by way, customer can have 1 order? o.o
Comments
Post a Comment