EDIT - Quick summary
I realized that I had quite a bit of text, so here's a quick summary of the issue:
<many-to-any />
is unsupported by Fluent NHibernate- Want to split property mapping between XML and Fluent to map a
<many-to-any />
relationship - Found out this is possible with named queries
- When trying to use both XML and Fluent for my interface, I get an
NHibernateDuplicateMappingException
- When trying to use only XML for the interface and Fluent for my subclasses, the XML map makes it to the database, but the Fluent is ignored.
Since I have a ton of conventions defined, I would prefer not to only use XML for this class hierarchy.
Also, if anyone knows of an alternative solution to using many-to-any in Fluent, I'd be more than willing to give it a try.
EDIT - Full Question and Code Samples
I have two classes (we'll call them "Foo" and "Bar") that share a many-to-many relationship. Usually, mapping this with Fluent this would be as simple an throwing HasManyToMany(x => x.SomeProperty)
into my class map; however, I've run into a problem with one of my interfaces.
For reference, here is a basic example setup of the objects:
public class Foo
{
public virtual long ID { get; set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual IList<IBar> Bars { get; set; }
}
public interface IBar
{
long ID { get; set; }
IList<Foo> Foos { get; set; }
}
Since IBar is an interface that can be attached to pretty much any class, any "BarID" that would be referenced on an intermediate table could belong to any number of other tables. Since this is a "many-to-any" relationship, it is still unsupported by Fluent (yes, I know this is supported by NHibernate's mapping-by-code).
Luckily, NHibernate's xml mappings can still be easily used in a project that uses Fluent. Rather than defining the entire map in the *.hbm.xml file, though, I only want to map the <many-to-any />
. The rest of it, I would like to keep in the Fluent map (basically, split the mapping out into two files).
I know this is possible with things like named queries and stored procedures, but I'm having a bit of trouble with using this pattern to define my relationship.
Here is the Fluent mapping for my Foo class as it is now:
public class FooMap : ClassMap<Foo>
{
public FooMap()
{
Id(x => x.ID);
Map(x => x.Name);
Map(x => x.Description);
}
}
And here the Foo.hbm.xml file:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MyProject" namespace="MyProject.Entities">
<class name="Foo" table="Foos">
<id name="ID">
<generator class="identity" />
</id>
<bag name="Bars" table="FooBarRelationship">
<key column="ID" />
<many-to-any id-type="System.Int64" meta-type="System.String">
<column name="BarID" />
<column name="BarType" />
</many-to-any>
</bag>
</class>
</hibernate-mapping>
On their own, both of these work fine. For example, if I'm just using the Fluent mapping, then my Foo table is generated with the three mapped columns. Similarly, if I just use the xml, then NHibernate creates a Foo table with an ID and a FooBarRelationship table, with both ID's, Foregin Keys, and the BarType column. Here's the problem, though: When I attempt to use both the xml and the Fluent mapping, I get an NHibernateDuplicateMappingException
. Additionally, if I only use XML for the interface, but use Fluent for the subclasses, the Fluent maps are completely ignored (i.e., it acts as if I only had the xml).
I've done quite a bit of searching, but I haven't been able to find any information that might help. Is there anything I can do to either maps (or even the configuration) to get this to work?