I had a similar problem when mapping from UserCountry table with userId (PK), countryId (FK) to Address table (one-to-many) userId (FK), countryId (FK) to get a unique address for a given UserCountry record.
In User mapping, I would get a unique address as
Code:
<many-to-one name="uniqueAddress" class="database.Address" property-ref="userIdCountryId">
<column name="user_id"></column> <!-- name of the foreign key column -->
<column name="country_id"></column> <!-- another foreign key column, the number of columns should match the property-ref -->
</many-to-one>
In the Address mapping, I would have the property-ref defined as userIdCountryId:
Code:
<!-- properties name, logical name of the grouping - not an actual name of the property
this name can be used as property-ref for ecField -->
<properties name="userIdCountryId">
<!-- the insert="false" and update="false" are needed as the fields are already used as properties -->
<property name="dummyUserId" type="long" insert="false" update="false">
<column name="user_id" ></column>
</property>
<property name="dummyCountryId" type="long" insert="false" update="false">
<column name="country_id" ></column>
</property>
</properties>
When I say
Code:
from User left join Address
where userId = 10
the sql query does join both of the columns from the user table to the address table, it nicely joins both the fields from user to address i.e.,
Code:
from User user left outer join Address address
on user.user_id = address.user_id and user.country_id = address.country_id
What is the difference between using one-to-one and many-to-one or are there any other options besides these?
When will the simple left join on any other class be introduced without having to introduce all these?