Hi,
I'm facing a problem in representing a 1:0..1 relationship.
Classes
There are 2 complex entities(classes) SubscriptionAccount & PaymentPlan having their own set of attributes. Both contain, NULL as well as NOT-NULL attributes. PaymentPlan contains another class, PaymentPlanItem.
The 1:0..1 relationship
The PaymentPlan will exist only if a SubcriptionAccount exists & it cannot exist on its own. Also, PaymentPlan may or, may not be null for a given SubscriptionAccount. Any PaymentPlan can have multiple PaymentPlanItems & again, PaymentPlanItems do not exist on their own.
Implementation
Instead of using the regular <one-to-one> representation of hibernate, I tried using the <component> representation, where a PaymentPlan will get represented as a component in SubscriptionAccount.
The config is done as specified below.
Code:
<class name="SubscriptionAccount" table="SUBSCRIPTION_ACCOUNTS">
<id name="subscriptionAccountID" column="SUBSCRIPTION_ACCOUNT_ID" type="MyGUIDType">
<generator class="MyGUIDGenerator"/>
</id>
<property column="CUSTOMER_ID" name="customerID" not-null="true" />
<property column="OFFER_LISTING_ID" name="offerListingID" not-null="true" />
<component name="paymentPlan" class="PaymentPlan" lazy="false" insert="true" update="true">
<property name="useGiftCertificateFlag" column="USE_GIFT_CERTIFICATE_FLAG"
type="yes_no" not-null="true" insert="true" update="true"/>
<property name="chargeAnyInstrumentFlag" column="CHARGE_ANY_INSTRUMENT_FLAG"
type="yes_no" not-null="true" insert="true" update="true"/>
<property name="paymentPlanGeneratorID" column="PAYMENT_PLAN_GENERATOR_ID"
not-null="false" insert="true" update="true"/>
<set cascade="all" inverse="false" lazy="true" name="paymentPlanItems" table="PAYMENT_PLAN_ITEMS"
access="property" outer-join="true">
<key column="SUBSCRIPTION_ACCOUNT_ID"/>
<composite-element class="PaymentPlanItem">
<property access="property" column="PAYMENT_INSTRUMENT_ID" insert="true" name="paymentInstrumentID" not-null="false" type="string" unique="false" update="true"/>
<property access="property" column="PAYMENT_PLAN_ITEM_PRECEDENCE" insert="true" name="paymentPlanItemPrecedence" not-null="false" type="int" unique="false" update="true"/>
</composite-element>
</set>
</component>
The domain classes as created as regular straightforward classes with getters and setters.
Questions
When i checked the logs the table SUBSCRIPTION_ACCOUNT was getting created with the columns ChargeAnyInstrument , useGiftCertificate as NOT NULL, which is not the expected behavior for my business model.
I expect the chargeAnyInstrumentFlag, useGiftCertificate or the PaymentPlanItems to be created only when a PaymentPlan is created & not for every instance creation of SubscriptionAccount.
In such case, Can I represent my required 1:0..1 relationship using a component or, going with a 1:0..1 is a must?
Summarising everything, can i represent a 1:0..1 with a component ? (or) can my component be null, whereas my component elements aren't ?
I would really really appreciate if someone can help me by answering my questions. A big thanks in advance
thanks,
Sriram