tvbusy
unread,Oct 24, 2008, 6:26:36 AM10/24/08Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Fluent NHibernate
I need to have the following mapping with Fluent NHibernate:
<map name="SamplesSelection" cascade="all-delete-orphan"
table="xxxxxx">
<key column="SanpshotID" />
<index-many-to-many column="AnalysisSampleID" class="xxxxxxxxx" />
<element column="SampleBottom" type="System.Single, mscorlib,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</map>
but couldn't find it, so I implemented it myself.
Class MapToManyPart.cs:
After AsBag method, add:
private string _valueColumn;
private System.Type _typeValue;
public ManyToManyPart<PARENT, CHILD> AsMapToValue<TValue>(string
valueColumn)
{
_valueColumn = valueColumn;
_typeValue = typeof(TValue);
_collectionType = "map";
return this;
}
Change implementation of Write method:
{
var conventions = visitor.Conventions;
string tableName = GetTableName(conventions);
string parentKeyName = GetParentKeyName(conventions);
string childForeignKeyName = GetChildKeyName(conventions);
XmlElement set =
classElement.AddElement(_collectionType).WithProperties(_properties);
set.WithAtt("table", tableName);
set.WithAtt("name", _property.Name);
XmlElement key = set.AddElement("key");
key.WithAtt("column", parentKeyName);
XmlElement manyToManyElement;
switch (_collectionType)
{
case "map":
manyToManyElement = set.AddElement("index-many-to-many");
XmlElement elementElement = set.AddElement("element");
elementElement.WithAtt("column", _valueColumn);
elementElement.WithAtt("type",
_typeValue.AssemblyQualifiedName);
break;
default:
manyToManyElement = set.AddElement("many-to-many");
break;
}
manyToManyElement.WithAtt("column", childForeignKeyName);
manyToManyElement.WithAtt("class",
typeof(CHILD).AssemblyQualifiedName);
manyToManyElement.WithProperties(_manyToManyProperties);
}
Test case:
Add property to MappedObject:
public IDictionary<ChildObject, float> ChildToValue { get; set; }
Test metho:
[Test]
public void ManyToManyAsMap()
{
new MappingTester<MappedObject>()
.ForMapping(m => m.HasManyToMany<ChildObject>(x =>
x.ChildToValue).AsMapToValue<float>("ChildValue"))
.Element("class/map")
.HasAttribute("name", "ChildToValue")
.HasAttribute("table", typeof(ChildObject).Name + "To" +
typeof(MappedObject).Name)
.Element("class/map/key")
.HasAttribute("column", "MappedObject_id")
.Element("class/map/index-many-to-many")
.HasAttribute("class",
typeof(ChildObject).AssemblyQualifiedName)
.HasAttribute("column", "ChildObject_id")
.Element("class/map/element")
.HasAttribute("column", "ChildValue")
.HasAttribute("type",
typeof(float).AssemblyQualifiedName);
}