-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Problem with one-to-one mapping and copmposite keys
PostPosted: Tue Sep 16, 2003 5:54 am 
Newbie

Joined: Tue Sep 02, 2003 9:17 am
Posts: 9
Location: London
Hi,

I have a Customer class that is keyed on a composite primary key. This key is implemented as per the documented recommendations as a separate entity. The Customer class also contains a many-to-one mapping keyed on the composite key. This works fine. However, what I would like to do is add a property to the Customer class in a different table. So for instance I have a separate table containing a composite foreign key, and one or more property columns. Looking at the Hibernate Wiki, the Delegate pattern seems to be a perfect fit. However, how do I specify an id of "foreign" or "assigned" when using composite keys? Here is my Customer mapping - is this the right way to go about it?

<hibernate-mapping>

<class name="uk.co.blueyonder.dialup.dao.Customer" table="CMS_ACCOUNTS">

<!-- The composite key -->
<composite-id name="id" class="uk.co.blueyonder.dialup.dao.CustomerCompositeId">
<key-property name="accountNumber" type="integer" column="Account_Number" />
<key-property name="siteID" type="integer" column="Site_ID" />
</composite-id>

<property name="password" column="Cms_Password" type="string" length="20" />
<property name="serviceType" column="Service_Type" type="string" length="1" />
<property name="lastBillValue" column="Last_Bill_Value" type="float" />
<property name="lastBillDate" column="Last_Bill_Date" type="date" />


<!-- The one-to-many mapping -->
<set name="telephoneAccounts" table="CMS_TELEPHONES" cascade="all">
<key>
<column name="Account_Number" />
<column name="Site_ID" />
</key>
<one-to-many class="uk.co.blueyonder.dialup.dao.TelephoneAccount" />
</set>

<!-- Would like to be able to access this as Customer.getProvisionStatus() -->
<one-to-one name="provisionStatus" class="uk.co.blueyonder.dialup.dao.DialupData" constrained="true" cascade="all" outer-join="true"/>
</class>

<!-- Here is the entity to delegate to -->
<class name="uk.co.blueyonder.dialup.dao.DialupData"
table="Dialup_Data"
proxy="uk.co.blueyonder.dialup.dao.DialupData">
<composite-id name="id" class="uk.co.blueyonder.dialup.dao.CustomerCompositeId">
<key-property name="accountNumber" type="integer" column="Account_Number" />
<key-property name="siteID" type="integer" column="Site_ID" />
</composite-id>
<!-- and the desired property -->
<property name="provisionStatus" column="Provision_Status" type="char"/>
</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject: Hibernate lazy instantiation problem
PostPosted: Tue Sep 16, 2003 6:39 am 
Newbie

Joined: Tue Sep 02, 2003 9:17 am
Posts: 9
Location: London
Hmmm....

When using the above mapping:

Customer c = new Customer();
CustomerCompositeId id = new CustomerCompositeId(1, 2); // comp. key

// ...set intrinsic Customer properties

DialupData d= new DialupData();
d.setId(id);
d.setProvisionStatus(status); // set the property

// Now add DialupData (one-to-one) to the customer
c.setProvisionStatus(d);

When I call save() on the customer, the associated DialupData entry is not created in the database. I can see Hibernate doing an UPDATE instead of an INSERT, and get the following error in the logs:

Hibernate lazy instantiation problem: No row exists

I presume this is because I have not mapped the foreign key correctly in DialupData (see previous post). Does anyone have any insight???


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 16, 2003 11:13 am 
Newbie

Joined: Tue Sep 02, 2003 9:17 am
Posts: 9
Location: London
In order to get the above mapping to work, I need to explicitly save the one-to-one mapped DialupData object, even though I have specified "cascade=all" in the mapping file. Does anyone know what I am doing wrong?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 16, 2003 12:46 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
unsaved-value="any"?

Please read the Parent/Child Relationship doco where it talks about how cascade-save interacts with composite-ids. There is also a note in the FAQ.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.