Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Trouble with composite keys and sets
PostPosted: Tue Sep 02, 2003 9:34 am 
Newbie

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

I'm having trouble getting Hibernate to process my mappings. I have a 2-table schema with a CUSTOMER_ACCOUNTS table keyed on a composite PK (Acount_Number, Site_ID). This is referenced by a CUSTOMER_TELEPHONES table, using the . The DDL is appended to the end of this email. My Customer mapping file looks like this:

<class name="uk.co.blueyonder.dialup.dao.Customer" table="CMS_ACCOUNTS">
<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"
<!-- More properties here .... -->
<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>
</class>

- So I would like to have a Set of TelephoneAccounts associated with each
Customer object, and pull them back with Customer.getTelephoneAccounts(). My TelephoneAccount.hbm.xml is shown here:

<class name="uk.co.blueyonder.dialup.dao.TelephoneAccount" table="CMS_TELEPHONES">
<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="unformattedTelephoneNumber" column="Unformatted_Telephone_Number" type="string" />
<property name="formattedTelephoneNumber" column="Formatted_Telephone_Number" type="string" />
</class>

Hibernate attempts to map these files, but it fails whenever it encounters the <set> declaration. No errors that I can see, it just seems to stop. (I'm looking at the Tomcat logs, BTW). If I take the <set> declaration out, it maps OK, but that's not what I want. Bearing in mind that the (Account_Number + Site_ID) combination will be unique in the CUSTOMER_ACCOUNTS table, but not in the CUSTOMER_TELEPHONES table (i.e one customer may have many telephone accounts), what am I doing incorrectly in the mapping? I'm sure I'm missing something painfully obvious - the <composite-id> declaration in the TelephoneAccount mapping, perhaps?

Here is the DDL:

create table CMS_ACCOUNTS (
Site_ID number(3) not null,
Account_Number number(9) not null,
Cms_Password varchar2(20),
Service_Type varchar2(1),
Last_Bill_Value number(9,2),
Last_bill_Date date,
Previous_Bill_Value number(9,2),
Previous_Bill_Date date,
Customer_Name varchar2(32),
Address_Line_1 varchar2(32),
Address_Line_2 varchar2(32),
Address_Line_3 varchar2(32),
Address_Line_4 varchar2(32),
Postcode varchar2(10)
);

create table CMS_TELEPHONES (
Site_ID number(3) not null,
Account_Number number(9) not null,
Unformatted_Telephone_Number varchar2(13),
Formatted_Telephone_Number varchar2(13)
);

alter table CMS_ACCOUNTS add constraint pk_cms_accounts primary key(Account_Number, Site_ID);
create index cms_telephony_idx on CMS_TELEPHONES(Account_Number, Site_ID);

Any help would be much appreciated

Cheers,
Rory


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 03, 2003 5:11 am 
Newbie

Joined: Tue Sep 02, 2003 9:17 am
Posts: 9
Location: London
I fixed the problem - it was a couple of issues. Firstly I took out the composite-id declaration in the TelephoneAccount.hbm.xml - after all, this may not be unique in this table, so it can't be a PK! Doh. So I added an integer PK to the table and mapped this to a named sequence like so:

<class name="uk.co.blueyonder.dialup.dao.TelephoneAccount" table="CMS_TELEPHONES">
<id name="id" column="ID" type="long">
<generator class="sequence">
<param name="sequence">telephone_account_seq</param>
</generator>
</id>
<property name="accountNumber" type="integer" column="Account_Number" />
<property name="siteID" type="integer" column="Site_ID" />
<property name="unformattedTelephoneNumber" column="Unformatted_Telephone_Number" type="string" />
<property name="formattedTelephoneNumber" column="Formatted_Telephone_Number" type="string" />
</class>


I also found that I was missing odmg.jar in my WEB-INF/lib, which explains why Hibernate bombed out when trying to process the <set> element.
Having done this, everything seems to be fine.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 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.