Reputation: 2708
I looked into nhibernate joins and couldn't figure out how the joins are performed. In the examples it doesn't provide the columns on which to perform the joins. Are these performed automatically based on the mappings?
What if I have 2 entities and have 2 separate relationships between them, this would mean that there multiple possibilities on joining the 2 entities?
Upvotes: 1
Views: 1123
Reputation: 123861
The answer is "these are performed automatically based on the mappings" (as you've stated).
Any C# object (POCO Entity) which is part of the NHibernate mapping, is explicitly connected to underling table(s) and columns. Any relation on the Entity level (C#) has its analogy in DB table relations.
The HQL example in documentation 14.3. Associations and joins:
from Eg.Cat as cat
join cat.Mate as mate
left join cat.Kittens as kitten
...is complete, and profits from the mapping. This provides sufficient information, how the Cat and Mate are related, how to join it to get list of Kittens
The mapping from documentation 5.1. Mapping declaration
<?xml version="1.0"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Eg"
namespace="Eg">
<class name="Cat" table="CATS" discriminator-value="C">
<id name="Id" column="uid" type="Int64">
<generator class="hilo"/>
</id>
<discriminator column="subclass" type="Char"/>
<property name="BirthDate" type="Date"/>
<property name="Color" not-null="true"/>
<property name="Sex" not-null="true" update="false"/>
<property name="Weight"/>
<many-to-one name="Mate" column="mate_id"/>
<set name="Kittens">
<key column="mother_id"/>
<one-to-many class="Cat"/>
</set>
<subclass name="DomesticCat" discriminator-value="D">
<property name="Name" type="String"/>
</subclass>
</class>
<class name="Dog">
<!-- mapping for Dog could go here -->
</class>
</hibernate-mapping>
The join. So if we go for a Mate, we will use the column mate_id
in table CATS
to get Mate from its table. When we need children, which are of the same type (Cat), we will go to table CATS
and search all records which have the column mother_id
equal to current id
column value.
So, "these are performed automatically based on the mappings"
Upvotes: 1