-->
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.  [ 8 posts ] 
Author Message
 Post subject: How to map two unrelated tables
PostPosted: Mon Nov 20, 2006 2:03 am 
Newbie

Joined: Mon Nov 20, 2006 1:48 am
Posts: 7
Location: Hyderabad
Hi guys, i'm facing a problem since 2 weeks.
I've two tables which have no relationship between them.

Table Account_Master(AccountNumber, MarketId, AccountType) where AccountNumber and MarketId are composite keys.

Table Call_Reason(AccountNumber, CallDate, CallReason, CallDesc) where AccountNumber and CallDate are composite keys.

I want to map a relationship between these tables based on AccountNumber.

Here are the mapping files.

Account_Master hbm file:

<class name="com.hibernate.pojos.AccountMaster" table="ACCOUNT_MASTER" schema="dbo" catalog="CNCHdb">
<composite-id name="id" class="com.hibernate.pojos.AccountMasterId">
<key-property name="marketId" type="string">
<column name="MarketId" length="5" />
</key-property>
<key-property name="accountNumber" type="integer">
<column name="AccountNumber" />
</key-property>
</composite-id>
<set name="callReasons" inverse="true" cascade="none" table="CALL_REASON" >
<key>
<column name="AccountNumber" not-null="true" />
</key>
<one-to-many class="com.hibernate.pojos.CallReason" />
</set>
</class>

Call_Reason hbm file:
<class name="com.hibernate.pojos.CallReason" table="CALL_REASON" schema="dbo" catalog="CNCHdb">
<composite-id name="id" class="com.hibernate.pojos.CallReasonId">
<key-property name="accountNumber" type="integer">
<column name="AccountNumber" />
</key-property>
<key-property name="callReasonDate" type="timestamp">
<column name="CallReasonDate" />
</key-property>
</composite-id>

<property name="callReasonNote" type="string">
<column name="CallReasonNote" not-null="true" />
</property>

<many-to-one name="accountmaster" class="com.hibernate.pojos.accountmaster" update="false" insert="false" fetch="select">
<column name="accountnumber" not-null="true" />
</many-to-one>

</class>

but i'm getting the error as:
org.hibernate.MappingException: Foreign key (FKAF34B45F2230FD:CALL_REASON [AccountNumber])) must have same number of columns as the referenced primary key (ACCOUNT_MASTER [MarketId,AccountNumber])
at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:90)
at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:73)
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1145)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1052)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1168)

Can anybody correct if i went wrong.
Please post me the as soon as possible.
Thanks in advance.

_________________
Kalyan Tilak M


Top
 Profile  
 
 Post subject: Re: How to map two unrelated tables
PostPosted: Mon Nov 20, 2006 5:00 am 
Newbie

Joined: Mon Nov 20, 2006 1:48 am
Posts: 7
Location: Hyderabad
tilakkalyan wrote:
Hi guys, i'm facing a problem since 2 weeks.
I've two tables which have no relationship between them.

Table Account_Master(AccountNumber, MarketId, AccountType) where AccountNumber and MarketId are composite keys.

Table Call_Reason(AccountNumber, CallDate, CallReason, CallDesc) where AccountNumber and CallDate are composite keys.

I want to map a relationship between these tables based on AccountNumber.

Here are the mapping files.

Account_Master hbm file:

<class name="com.hibernate.pojos.AccountMaster" table="ACCOUNT_MASTER" schema="dbo" catalog="CNCHdb">
<composite-id name="id" class="com.hibernate.pojos.AccountMasterId">
<key-property name="marketId" type="string">
<column name="MarketId" length="5" />
</key-property>
<key-property name="accountNumber" type="integer">
<column name="AccountNumber" />
</key-property>
</composite-id>
<set name="callReasons" inverse="true" cascade="none" table="CALL_REASON" >
<key>
<column name="AccountNumber" not-null="true" />
</key>
<one-to-many class="com.hibernate.pojos.CallReason" />
</set>
</class>

Call_Reason hbm file:
<class name="com.hibernate.pojos.CallReason" table="CALL_REASON" schema="dbo" catalog="CNCHdb">
<composite-id name="id" class="com.hibernate.pojos.CallReasonId">
<key-property name="accountNumber" type="integer">
<column name="AccountNumber" />
</key-property>
<key-property name="callReasonDate" type="timestamp">
<column name="CallReasonDate" />
</key-property>
</composite-id>

<property name="callReasonNote" type="string">
<column name="CallReasonNote" not-null="true" />
</property>

<many-to-one name="accountmaster" class="com.hibernate.pojos.accountmaster" update="false" insert="false" fetch="select">
<column name="accountnumber" not-null="true" />
</many-to-one>

</class>

but i'm getting the error as:
org.hibernate.MappingException: Foreign key (FKAF34B45F2230FD:CALL_REASON [AccountNumber])) must have same number of columns as the referenced primary key (ACCOUNT_MASTER [MarketId,AccountNumber])
at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:90)
at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:73)
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1145)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1052)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1168)

Can anybody correct if i went wrong.
Please post me the as soon as possible.
Thanks in advance.

_________________
Kalyan Tilak M


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 20, 2006 8:14 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
You cannot map a relation to a table without specifying the whole primary key columns.

If you have a table A(c1,c2,c3) with primary_key(C1,C2) all tables referencing A will have to have TWO columns to reference both foreign columns.

This is a relational rule, not an hibernate one.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 20, 2006 8:18 am 
Newbie

Joined: Mon Nov 20, 2006 1:48 am
Posts: 7
Location: Hyderabad
But we have a database desing like that. We have two tables with the below given situation. Thats why i posted the issue. If at all we get that type of situation how can we handle.


batmat wrote:
You cannot map a relation to a table without specifying the whole primary key columns.

If you have a table A(c1,c2,c3) with primary_key(C1,C2) all tables referencing A will have to have TWO columns to reference both foreign columns.

This is a relational rule, not an hibernate one.

_________________
Kalyan Tilak M


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 20, 2006 8:18 am 
Newbie

Joined: Mon Nov 20, 2006 1:48 am
Posts: 7
Location: Hyderabad
But we have a database desing like that. We have two tables with the below given situation. Thats why i posted the issue. If at all we get that type of situation how can we handle.


batmat wrote:
You cannot map a relation to a table without specifying the whole primary key columns.

If you have a table A(c1,c2,c3) with primary_key(C1,C2) all tables referencing A will have to have TWO columns to reference both foreign columns.

This is a relational rule, not an hibernate one.

_________________
Kalyan Tilak M


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 20, 2006 10:24 am 
Newbie

Joined: Mon Nov 13, 2006 11:49 am
Posts: 6
You must specify primay and foreign key constraints in order for Hibernate to see the relationship that is implied.

Hibernate can't guess at a relationship between tables. I had the same problem recently with a legacy DB that our client requires that we use.

What we did was go through the DB, specify constraints, then generate Hibernate mappings. There are some potentail hicups and some places in the DB that aren't easily reached, but thats the downfall of using new technology like Hibernate on an old poorly designed DB.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 14, 2006 8:25 am 
Newbie

Joined: Mon Nov 20, 2006 1:48 am
Posts: 7
Location: Hyderabad
Can you please update me the xml mappings files which i have posted.
Its very urgent.

psuross wrote:
You must specify primay and foreign key constraints in order for Hibernate to see the relationship that is implied.

Hibernate can't guess at a relationship between tables. I had the same problem recently with a legacy DB that our client requires that we use.

What we did was go through the DB, specify constraints, then generate Hibernate mappings. There are some potentail hicups and some places in the DB that aren't easily reached, but thats the downfall of using new technology like Hibernate on an old poorly designed DB.

_________________
Kalyan Tilak M


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 14, 2006 8:32 am 
Newbie

Joined: Mon Nov 20, 2006 1:48 am
Posts: 7
Location: Hyderabad
Can you please update me the xml mappings files which i have posted.
Its very urgent.

psuross wrote:
You must specify primay and foreign key constraints in order for Hibernate to see the relationship that is implied.

Hibernate can't guess at a relationship between tables. I had the same problem recently with a legacy DB that our client requires that we use.

What we did was go through the DB, specify constraints, then generate Hibernate mappings. There are some potentail hicups and some places in the DB that aren't easily reached, but thats the downfall of using new technology like Hibernate on an old poorly designed DB.

_________________
Kalyan Tilak M


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.