I am having trouble mapping a collection of Bags for a Child Class (Sales Account Summary History) to a Parent Class (Sales Account Summary).
Each Sales Account Summary can have multiple Sales Account Summary Histories (so a 1:N relationship)
Problem:
It works perfectly when I do a saveorUpdate (meaning each element in the collection is saved properly).
When I load a Sales Account Summary, I receive the correct number of Adjustments, but the details of each adjustment are equal to the latest (or newest) adjustment so they are all the same. They are different in the database however which I confirmed.
:( Any feedback is greatly appreciated!
I have 2 tables: sales_account_summary and sales_account_summary_history
The following are the primary key structures of the tables.
Table: sales_account_summary
Code:
PK sales_account_id
PK yearmo
....
Table: sales_account_summary_historyCode:
PK sales_account_history_id
Blind FK sales_account_id
Blind FK yearmo
...
SalesAccountSummary MappingCode:
<hibernate-mapping>
<class name="SalesAccountSummary" table="dbo.sales_account_summary" lazy="false">
<composite-id>
<key-property name="salesAccountId" column="sales_account_id" />
<key-property name="yearMo" column="yearmo" />
</composite-id>
<property name="commissionEarned" column="commission_earned" />
<property name="commissionPosted" column="commission_posted" />
<property name="directCost" column="direct_cost" />
<property name="directSales" column="direct_sales" />
<property name="stockCost" column="stock_cost" />
<property name="stockSales" column="stock_sales" />
<property name="adjustmentReason" column="change_reason" />
<property name="adjustmentUsername" column="username" />
<!-- This is not working for some reason - not giving unique results-->
<bag name="adjustmentHistory" lazy="false">
<key>
<column name="sales_account_id" not-null="true"/>
<column name="yearmo" not-null="true"/>
</key>
<one-to-many class="SalesAccountSummaryAdjustment" />
</bag>
</class>
</hibernate-mapping>
SalesAccountSummaryAdjustment MappingCode:
<hibernate-mapping>
<class name="SalesAccountSummaryAdjustment" table="dbo.sales_account_summary_history" lazy="false">
<composite-id>
<key-property name="salesAccountId" column="sales_account_id" />
<key-property name="yearmo" column="yearmo" />
</composite-id>
<property name="id" column="sales_account_history_id"/>
<property name="oldCommissionEarned" column="old_commission_earned"/>
<property name="oldCommissionPosted" column="old_commission_posted"/>
<property name="changeReason" column="change_reason" />
<property name="timestamp" column="timestamp"/>
<property name="username" column="username" />
</class>
</hibernate-mapping>
An example of getting a Sales Account Summary Code:
public SalesAccountSummary retrieveSalesAccountSummary(int accountId, int yearMo) {
Session session = HibernateUtil.getCurrentSession();
session.beginTransaction();
Criteria crit = session.createCriteria(SalesAccountSummary.class);
crit.add(Expression.eq("salesAccountId", new Integer( accountId )));
crit.add(Expression.eq("yearMo", new Integer( yearMo )));
SalesAccountSummary summary = (SalesAccountSummary)crit.uniqueResult();
session.getTransaction().commit();
return summary;
}