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.