Using Embeddable, Embedded, and Secondary Table instead One-to-One (in Hibernate)
In my last blog entry, I discussed One-to-One relationship in Hibernate. The problem was the extra SQL statements! One solution is to use Embeddable and Embedded concepts.
The ProductDescription becomes an Embeddable instead of an entity. Other than that, it pretty much remains the same as shown below.
1: @Embeddable
2: public class ProductDescription implements Serializable {
3: @Column(name = "DESCRIPTION", table = "PRODUCT_DESC"
4: private String mDescrption;
5:
6: ...
7: }
The Product instead of having one-to-one, has an embedded ProductionDescription. I also indicate the secondary table with @SecondaryTable annotation.
1: @Entity
2: @Table(name = "PRODUCT")
3: @SecondaryTable(name = "PRODUCT_DESC")
4: public class Product {
5: ...
6:
7: @Embedded
8: private ProductDescription mProductDescription;
9:
10: ...
11: }
Running with the same example as in the previous blog entry, the SQL generated includes an outer join and the number of SQL statements is reduced!
1: select
2: product0_.PRODUCT_ID as PRODUCT1_0_, product0_1_.DESCRIPTION as DESCRIPT1_1_
3: from
4: PRODUCT product0_
5: left outer join PRODUCT_DESC product0_1_ on product0_.PRODUCT_ID=product0_1_.PRODUCT_ID
In the previous blog entry, I mentioned, almost lamented, about all those blog entries and forum posts that describe the problem, but no decent solution. Now, you can include this blog entry to that list!

Hi, I cannot find this type of relation ship in JEE spec. Is this a supported feature of JEE or Hibernate, or it just happens to work?
Thanks
Reply to this
This is part of the Java Persistence API specification. I just downloaded the final JSR 220 specification. Section 9.2.2 has an example of Employee and EmploeePeriod.
Best regards,
+prakash
Reply to this