So I've been searching for hours how to fix this legacy problem:
I have two physical tables, but 3 domain objects. Here are my tables (They have been simplified):
Account
Account Number (PK)
User
Account Number (PK)
User ID (PK)
Product Use (We will not need to look at this one)
Product Id (PK)
User ID
Account Number
Usage
Product Name
User and Product Use are all from the same table. I need to use Account Number and User ID as primary keys of User because the same user id is used for multiple accounts (legacy!).
Here is what my mapping looks like so far:
Code:
<class name="Account" table="ACCOUNT">
<id name="accountNumber" column="ACCOUNT_NUMBER"></id>
<bag name="users" lazy="true" inverse="false" table="BIG_TABLE">
<key column="ACCOUNT_NUM" not-null="true" />
<one-to-many class="User"/>
</bag>
</class>
......
<class name="User" table="BIG_TABLE">
<subselect>
select distinct USER_ID, ACCOUNT_NUM
from BIG_TABLE
</subselect>
<composite-id>
<key-property name="userId" column="USER_ID" />
<key-property name="accountNumber" column="ACCOUNT_NUM" />
</composite-id>
...
</class>
This fails, giving me a "Repeated column mapping for entity: User" error.
The problem is trying to use a field mapped as my composite key (User) as a foreign key (Account bag).
How would I get around this? Please remember I'm unable to change anything on the database side of things.
Thanks!