Joins the fluent nhibernate
I am using fluent nhibernate.
Example:
I have 3 ie tables
CUSTOMER CustomerId pk CustomerName
PRODUCT ProductId pk ProductName
Cust_Product cust_prodId pk ProductId fk CustomerId fk
Now I want to show the username, productnae
the way I write the mapping class for the same.
I want to use
session.CreateCriteria (TypeOf ("class name")). List () like this. how do i do it ??
source to share
If you're looking for a complete tutorial on how to do this, I recommend the FNH wiki or one of the many blog posts you can find via Google.
However, you are trying to implement a many-to-many relationship here and that seems to throw a lot of people off. Here's an example:
In your Customer class, you need a collection, for example:
IList<Product> Products { get; private set; }
And similarly, in your Product class:
IList<Customers> Customers { get; private set; }
You start off with a many-to-many map with a function HasManyToMany
:
public class CustomerMap : ClassMap<Customer>
{
public CustomerMap()
{
// other mappings
HasManyToMany<Product>(x => x.Products)
.WithTableName("Cust_Product") // Specifies the join table name
.WithParentKeyColumn("CustomerId") // Specifies the key joining back to this table (defaults to [class]_id, Customer_id in this case)
.WithChildKeyColumn("ProductId")
.FetchType.Join(); // Instructs NHibernate to use a join instead of sequential select
}
}
Then repeat the process for the other side of the relationship (the Customers property in the Product class).
source to share