Map and index-many-to-many

6 views
Skip to first unread message

tvbusy

unread,
Oct 24, 2008, 6:26:36 AM10/24/08
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);
}

Paul Batum

unread,
Oct 28, 2008, 11:35:08 AM10/28/08
to fluent-n...@googlegroups.com
Hi there,

Could you submit this change in patch form? If you do so, we could look at applying it to the trunk.

Thanks,

Paul Batum

Ngoc Van Tran

unread,
Oct 28, 2008, 11:23:59 PM10/28/08
to fluent-n...@googlegroups.com
I've attached the patch file with this message.
Map for index-many-to-many.patch

Paul Batum

unread,
Nov 1, 2008, 12:07:47 PM11/1/08
to fluent-n...@googlegroups.com
Hi Ngoc,

Thanks for your submitted patch. I will take a look sometime in the coming week.

Paul Batum
Reply all
Reply to author
Forward
0 new messages