Using subselect instead of views
Views cater to the situation where we want to read data from multiple legacy tables and map it to a single domain entity. But under the hood, view is just a SELECT
query. NHibernate offers another way to deal with the SELECT
query directly, so that there is no need to have a view created in the database. This can be useful when you are not able to create views in the database for some reason. Some RDMBS have limited support for views, in which case we could use a subselect feature.
In a subselect feature, we would use the same query that we used to create the view, but this time, we will map to the entity directly. The following mapping definition demonstrates this:
public class EmployeeSubselectMapping : ClassMapping<Employee> { public EmployeeSubselectMapping() { Subselect(@"SELECT dbo.Employee.Id, dbo.Employee.Firstname, dbo.Employee.Lastname, dbo.Employee.DateOfJoining, dbo.Address.AddressLine1, dbo.Address.AddressLine2 FROM dbo...