One-to-One in Hibernate

Consider the following one-to-one relationship between Product and ProductDescription: Product can have optional ProductDescription. The following is the Product side:

   1: public class Product {
   2:     ...
   3:  
   4:     @OneToOne(mappedBy = "mProduct", fetch = FetchType.LAZY, optional = true)
   5:     private ProductDescription mDescription;
   6:  
   7:     ...
   8: }

The following is the ProductDescription side:

   1: public class ProductDescription {
   2:     ...
   3:  
   4:     @OneToOne(optional = false)
   5:     @JoinColumn(name = "PRODUCT_ID")
   6:     private Product mProduct;
   7:  
   8:     ...
   9: }

Assuming the product table contains two products. One without description and one with description. Hibernate generates the following SQL:

   1: select product0_.PRODUCT_ID as PRODUCT1_0_ from PRODUCT product0_
   2: select productdes0_.PRODUCT_ID as PRODUCT1_1_1_, productdes0_.DESCRIPTION as DESCRIPT2_1_1_, product1_.PRODUCT_ID as PRODUCT1_0_0_ from PRODUCT_DESC productdes0_ inner join PRODUCT product1_ on productdes0_.PRODUCT_ID=product1_.PRODUCT_ID where productdes0_.PRODUCT_ID=?
   3: select productdes0_.PRODUCT_ID as PRODUCT1_1_1_, productdes0_.DESCRIPTION as DESCRIPT2_1_1_, product1_.PRODUCT_ID as PRODUCT1_0_0_ from PRODUCT_DESC productdes0_ inner join PRODUCT product1_ on productdes0_.PRODUCT_ID=product1_.PRODUCT_ID where productdes0_.PRODUCT_ID=?

The ProductDescription in Product is lazy, so why does hibernate try to load the descriptions?

The are many blogs, forum messages, etc. for this problem. They describe hacks and other ideas, but none of them is quite acceptable.

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name (required)

 Email (will not be published) (required)

 Website

Your comment is 0 characters limited to 3000 characters.