NHibernate Mapping-by-code One-To-One relationship: fetch by Join? - nhibernate

I have a table (UncommittedVideoFile) that has a strict one-to-one relationship with a different table (VideoOrImageAsset). When I load an UncommittedVideoFile, I want to load the VideoOrImageAsset in the same query via a JOIN. (Inner or left outer).
Apparently it's possible to specify fetch-by-join when you use xml mapping: https://ayende.com/blog/3960/nhibernate-mapping-one-to-one
It's also possible if you are using Fluent NHibernate: https://stackoverflow.com/a/15694723/25216
But using MappingByCode, is is possible to set a Fetch option? I can't see one anywhere.
Here is the code that I currently have. The IOneToOneMapper interface does not have a Fetch method.
classMapper.OneToOne(
f => f.VideoOrImageAsset,
oneToOneMapper =>
{
oneToOneMapper.Constrained(true);
oneToOneMapper.ForeignKey("Id");
oneToOneMapper.Lazy(LazyRelation.NoLazy);
}
);

It's known missing mapping-by-code feature GH-2139. And it's already implemented in 5.3 (not yet released, but pretty close)
For now you can use pre-release version of 5.3 from https://www.myget.org/gallery/nhibernate (also check notes how to use it here)

Related

Fetching a lazy loaded property in NHibernate that *isn't* a reference

I have an entity with a binary column that's set to lazy load in the mapping. In some cases, though, we want to get the entity along with the binary data at the same time. I tried using Linq.Fetch(x => x.BinaryData), but that gives an invalid join exception. Understandable, considering it shouldn't be a join in the first place. Is there a way to get this working? I'm using NHibernate 3.1
This is the mapping:
Map(x => x.BinaryData)
.CustomSqlType("image")
.Length(int.MaxValue)
.Not.Nullable()
.LazyLoad(); // Wanna make sure we don't kill the app by loading the image data when we don't need it.
This is the fetching:
Linq.Where(x => x.Id == id).Fetch(x => x.BinaryData).FirstOrDefault();
This looks like to be not possible at the moment : https://nhibernate.jira.com/browse/NH-2888
So, You have to use HQL :
var post = session.CreateQuery("from Post fetch all properties")
.SetMaxResults(1)
.UniqueResult<Post>();
Source : http://ayende.com/blog/4377/nhibernate-new-feature-lazy-properties
In HQL you can use fetch all properties to eagerly load lazy property. But in NH3.1 it is not yet implemented for Linq queries. As I know this bug is in NHibernate Jira so you can check if it is resolved or you can fix it yourself. For our company prototype i fixed this bug, but I did so in very brute-force way so i didn't send patch to NHibernate project

FluentNHibernate: LazyLoad and Fetch

in fluent nhibernate I can set Fetch.Something and Not.LazyLoad to a Reference or HasMany. What will happen if I use both?
How these two reflects to querying data in these three ways?
class UserMap
{
HasMany(x=>x.Contacts). (Fetch or Not.LazyLoad)
References(x=>x.Supervisor). (Fetch or Not.LazyLoad)
}
session.Query<User>();
session.Query<User>().FetchMany(x=>x.Contacts);
session.Get<User>(ID);
The problem is that Fetch is not taken into account for Query/HQL. So, immediately after running the query, it will try to fetch your Not.LazyLoad properties one by one.
In general, disabling lazy loading is a bad idea in 99% of the cases. Suggested read: NHibernate is lazy, just live with it

Fetch.Join() not working in Fluent NHibernate

I have the following mapping override on one side of the relationship:
public void Override(AutoMapping<ItemAsmtDetailDh> mapping)
{
mapping.HasMany<WAsmtDetail>(x => x.WAsmtDetails).Inverse().AsBag().Cascade.AllDeleteOrphan().Access.PascalCaseField(Prefix.Underscore).Not.LazyLoad().Fetch.Join();
}
and on the other side of the relationship I have:
public void Override(AutoMapping<WAsmtDetail> mapping)
{
mapping.References<ItemAsmtDetailDh>(x => x.ItemAsmtDetailDh).Not.Nullable().Not.LazyLoad().Fetch.Join();
}
When I use the ShowSql option I see that it's still issuing separate select statements for the WAsmtDetails giving me the dreaded "n + 1 selects" problem. Why is ".Not.LazyLoad().Fetch.Join()" being ignored?
Note: I'm using Fluent NHibernate version 1.1, not version 2.1, because of a bug in the newer version. (See my answer for this question for bug details.) I'm using NHibernate version 2.1.2.4000.
You are most likely loading the data in a way that isn't affected by Fetch.Join() in the mapping (like HQL or Linq). From the NHibernate documentation:
The fetch strategy defined in the mapping document affects:
retrieval via Get() or Load()
retrieval that happens implicitly when an association is navigated
ICriteria queries
HQL queries if subselect fetching is used

How do I use Fluent Nhibernate many-to-many for optimal performance?

I have a product table that has a many-to-many relation to itself (using a two-column many-to-many table) and I have set it up in Fluent NHibernate with the following code:
public class ProductConfiguration : ClassMap<Product>
{
public ProductConfiguration()
{
Table("Product");
Id(p => p.Id).GeneratedBy.Guid();
Map(p => p.Name).Not.Nullable().Length(254);
Map(p => p.Description).Not.Nullable().Length(1000);
Map(p => p.CreatedAt).Not.Nullable();
HasManyToMany(p => p.CrossSell)
.Table("ProductCrossSell")
.ParentKeyColumn("Id")
.ChildKeyColumn("ProductId");
}
}
My MVC application has two pages that uses this setup:
Index - Uses a generic repository GetAll method to display all products.
Detail - Uses a generic repository GetById method to display one product and any related cross sell products setup in the many-to-many realation.
It looks like NHibernate is set to LazyLoad the many-to-many by default so when I fire up the application and watch it in profiler I can see that it does LazyLoad the many-to-many with the following alert "Use of implicit transactions is discouraged".
How do I get rid of this alert? I couldn't find any information on how to wrap a LazyLoad inside a transaction to get rid the alert. Is it even possible?
Is there a way to not lazyload this by telling NHibernate that whenever I ask for GetById make sure to join the tables a get everything in one query? I tried using .Fetch.Join() in the many-to-many mapping but that also affected my GetAll query which now displays a joined result set as well which is incorrect.
What is the best apprach for this kind of simple scenario?
Thanks
The way to get rid of the warning is to access the object graph and fully populate the UI elements inside a single transaction.
Not by configuration. You can create an HQL query that eager fetches the association and use that query for a specific view. I would stick with lazy loading and not make that optimization unless needed. The HQL would be:
return session.CreateQuery("from ProductionConfiguration pc join fetch pc.CrossSell where pc.Id = ?")
.SetGuid(0, id)
.List<ProductConfiguration>();
All collections are lazily loaded in NHibernate by default.
You must be triggering loading with a call of some kind (maybe even with the debugger watches)

NHibernate - Incorrect thinking? Subclassed Model based on Join

I have a simple model class (Part), which pulls from it's information from a single table (t_Part).
I would like a subclass of this model called (ProducedPart), that would still utilize NHibernate's caching mechanisms, but would only be instances of (Part) that have a foreign key relationship in a table called "t_PartProduction". I do not need to have a model for this second table.
I only need a read-only version of ProducedPart
I could always implement a Facade/Repository over this, but I was hoping to setup a mapping that would pull "t_Part" joined with "PartProduction" when I asked for "ProducedPart" in NH.
Is this the wrong way to use NH?
Edit
So, the SQL would look something like
SELECT p.*
FROM t_Part p
INNER JOIN t_PartProduction pp ON pp.PartID = p.PartID
WHERE pp.ProductionYear = '2009'
I believe what you are looking for is a joined subclass. In FNH, it will look something like:
public class PartMap : ClassMap<Part>
{
public PartMap()
{
Id(x => x.Id)
JoinedSubClass<ProducedPart>("PartID", sub => {
sub.Map(x => x.Name);
sub.Map(x => x.ProductionYear);
});
}
}
In order have NHibernate cache the results, you will need to have the subclass mapped (and if you didn't map it, you wouldn't be able to get NH to load it in the first place).
Bringing in some context from the FNH groups thread, it will not explicitly be read-only though. In my opinion, making things read-only is not an appropriate thing for NHibernate to manage. This is better controlled by the database and connections (i.e. creating a connection to the database that only has SELECT permissions on the tables/views being accessed). See my answer to a previous SO question about readonly sessions in NHibernate for more of my thoughts on the matter.
The key here is using both the where and mutable elements of the class definition for NHibernate Mappings.
Using Fluent NHibernate, this looks like:
public Part()
{
WithTable("t_Part");
Id(i => i.Id).ColumnName("PartID");
Map(m => m.Name).ColumnName("Part");
SetAttribute("where", "PartID IN ( SELECT pp.PartID FROM t_PartProduction pp WHERE pp.ProductionYear = '2009' ) ");
ReadOnly();
}
No, this is perfectly possible. Look in the NHibernate documentation for the "table per subclass" model of inheritance. It will actually implement this as a LEFT JOIN, so that when you load a Part, it creates an instance of either your Part or your ProducedPart class depending on whether the other row is present. You'll find documentation on nhibernate.info.
I'm not sure you could make ProducedPart read-only doing this though.
I'm assuming from this:
WHERE pp.ProductionYear = '2009'
that you want the subclass only where the production year is 2009, i.e. if when there is a record in t_PartProduction for a different year, you want this Part treated as a plain Part object, not a ProducedPart, then you could consider creating a view definition within your database that is a filtered version of t_PartProduction, then making your subclass join to this view rather than the base table.