-->
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.  [ 1 post ] 
Author Message
 Post subject: strange behaviour when updating, foreign keys set to null
PostPosted: Wed Jan 23, 2008 11:37 am 
Newbie

Joined: Wed Jan 23, 2008 11:03 am
Posts: 2
hello

my problem lies within these three tables in my stock exchange server

    *Companies: table of stock companies
    *Accounts: user accounts; each account contains a set of shares
    *Shares: each share belongs to one company (and each company has a set of shares). Also each share belongs to one account.

The following hbms show these associations:

Companie.hbm.xml
Code:
..
<set name="shares" cascade="all" inverse="true">
            <key column="companyId" />
            <one-to-many class="Share"/>
</set>
..

Account.hbm.xml
Code:
..
<set name="shares" inverse="true">
   <key column="accountId"/>
   <one-to-many class="Share" />
</set>
..


Share.hbm.xml
.
Code:
.
<property name="price"/>
<property name="transDate"/>
<many-to-one name="company" column="companyId"/>
<many-to-one name="account" column="accountId"/>
..


when there's a shares transaction, I take all the sold shares and update their account so they will belong to the new user account.


Code:
Session s = ...
Transaction tx = s.beginTransaction();
for(Share share: shares) {
  share.setAccount(newAccount); //here replace old account with new one; works correctly
  System.err.println(share.getCompany()); // shows the company correctly
  session.save(share);
}
tx.commit();

for(Share share: shares) {
  System.err.println(share.getAccount()); // prints newAccount
  System.err.println(share.getCompany()); // prints null !!!!!! <<<<<<<
}
s.close();


The problem is that all the sold shares will lose their associated companies after this update (but the code change the account correctly). As you can see I have not modified the Company, only the account. I added a syserr to show the share's company (syserr(share.getCompany()) and it prints correctly the name of the company; it's not null. Why hibernate makes the Company null for all sold shares when the transaction is commited?

one last hint, when I remove the one-to-many association in companies, it works fine; but I need this association in other places :S

thank you for answers in advance. I'll be happy to give you more information if you would like to.


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

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.