I’ve a class with many relations mapped.
One of those relations should be fetched for a concrete user.
public class Company
{
public int id { get; set; }
public string Name { get; set; }
public IList<StocksPack> stocks { get; set; }
// a lot of other properties referencing other objects
}
public class StocksPack
{
public int stockId { get; set; }
public int companyId { get; set; }
public int amount { get; set; }
public int ownerId { get; set; }
}
How Do I map the stocks to be left-outer-joined
ON stocks.companyId = company.id AND stocks.ownerId = 123456
Where the actual ownerId is known only at runtime, after the user has logged on to the system.
I want to get all the info about the company and a list of Stocks of that company for the concrete user.
I think the premise of your question is mistaken: you don’t map objects on left outer joins. You map them with in a database with a foreign key reference or in nhibernate with a class map like the following:
Where stock looked like:
And you had some Company object with a map set up that gave it an id field.
This is the simple parent child id type relationship you have with stock and company. I’m assuming.
The idea behind hibernate and nhibernate is you don’t have to have the id references in your objects. It handles that for you
The left outer join only enters in when you query the data. Put another way, the mapping is the relationship between the two types, the query is a statement about how you want that relationship represented and what data you want. The former is a property of your data model and system design, the later some event that happens at runtime.
There is some ambiguity on what you said you want the query to do, but it appears you want to list all the stocks and, when that stock is owned by a certain owner, the company for that stock as well. The other possibility is that you wanted to list all the companies and , when they have stock owned by a specific id, that stock as well.
In either case, you want a simple left join query. In fluent bhibernate these look like this:
In C# using linq you can use this example: