Complicated NHibernate component mapping - nhibernate

EDIT: I simplified the problem to leave only what really bothers me.
Hello all,
I am trying to make the following mapping.
In my database, I have a table called "ReportRowValue" containg the following columns:
RowNumber
ColumnNumber
StringValue
LongValue
DateValue
Value
In my code I want to get a more usable structure by creating several two classes from this one table. I guess this should be done using components and inheritance but I did not managed to create a working mapping file. What I want in code should look like this:
ReportRow
RowNumber
Values (collection of ReportValue below)
ReportValue (being an abstract class)
ColumnNumber
Value
ReportValueString / ReportValueLong / ReportValueDate (each one inheriting from ReportValue)
Value (each one having a Value property of its one type)
And that's about all!
Does anyone can point me how to create an nhibernate mapping file/files for doing that?
Thanks,
Meigetsu

There is couple of tools that maps and builds class for you one of them is
mygeneration
is the software http://sourceforge.net/projects/mygeneration/
In this page you find the templates that you need to run with the
softwarehttp://www.mygenerationsoftware.com/TemplateLibrary/Archives/?query=nhibernate
After you have this in the mygeneration tool you only connect to your DB and it will generated for you

Unfortunately, you can't have a polymorphic structure in a component. But I'm acutally not sure if you need it.
The following code is straight from my head, so it certainly has errors or missing things and wouldn't compile. But it should show the direction:
public class ReportRow
{
public int Id { get; private set; }
public IList<IReportValue> Values { get; private set; }
}
public interface IReportValue
{
public int Id{ get; set; }
public object UntypedValue { get; }
}
public abstract class ReportValue<T> : IReportValue
{
public int Id{ get; set; }
public T Value { get; set; }
public object UntypedValue { get { return Value; } }
}
public class ReportLongValue : ReportValue<long> {}
public class ReportStringValue : ReportValue<string> {}
public class ReportDateValue : ReportValue<DateTime>{}
Mapping:
<class ReportRow>
<id ...>
<bag name="Values" >
<key column="RowNumber"/>
<one-to-many class="IReportValue"/>
</bag>
</class>
<class name="IReportValue" abstract="true">
<id ...>
<subclass name="ReportLongValue">
<property name="Value" column="LongValue"/>
</subclass>
<subclass name="ReportStringValue">
<property name="Value" column="StringValue"/>
</subclass>
<subclass name="ReportDateValue">
<property name="Value" column="DateValue"/>
</subclass>
</class>

Related

NHibernate mapping

I use HBM mapping.
I have tables :
I) person with columns :
1. ID
2. TYPE
3.CREATE_DATE
4.UPDATE_DATE
II) Attribute with columns:
1.ID
2.TYPE(in this example person may be all type)
3.NAME
4.CREATE_DATE
5.UPDATE_DATE
III) Attribute_VALUE with columns:
1.ID
2.VALUE
4.OBJECT_ID
5.ATTRIBUTE_ID
6.CREATE_DATE
7.UPDATE_DATE
There is relationship one-to-many between person(ID) and Attribute_VALUE(OBJECT_ID).
There is relationship one-to-many between Attribute(ID) and Attribute_VALUE(ATTRIBUTE_ID)
I need build object PERSON that contain all columns of person and dictionary with name attribute.
The dictionary contain key - name of attribute value- collection of values .
Can I build appropriate HBM ??
the short answer no.
the long answer:
consider how should nhibernate match attributes when you Attributes.Add("foo", "value")? it has to search the db for an attribute foo (which is not a simple mapping, its logic) or it would create a new Attribute, everytime you add one.
So given the above schema you either a) have some kind of custom onsave code (which i think is a lot of effort) or b) you change the Person to
class Person
{
public virtual int Id { get; set; }
public virtual ICollection<AttributeValue> Attributes { get; set; }
public virtual IEnumerable<string> GetValues(string attributeName)
{
return Attributes
.Where(attr => attr.Attribute.Name == attributeName)
.Select(attr => attr.Value);
}
public virtual void AddValue(Attribute attribute, string value)
{
Attributes.Add(new AttributeValue
{
Attribute = attribute,
Value = value
});
}
public virtual IEnumerable<string> GetAttributeNames()
{
return Attributes
.Select(attr => attr.Attribute.Name);
}
}
class Attribute
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
// and more Properties like created and updated
}
class AttributeValue
{
public virtual int Id { get; set; }
public virtual Attribute Attribute { get; set; }
public virtual string Value { get; set; }
// and more Properties like created and updated
}
and then use
<class name="Person" table="Persons" xmlns="urn:nhibernate-mapping-2.2">
<id name="Id" column="ID"/>
<bag name="Attributes">
<key column="OBJECT_ID"/>
<one-to-many class="AttributeValue"/>
</bag>
</class>
<class name="Attribute" table="Attributes" xmlns="urn:nhibernate-mapping-2.2">
<id name="Id" column="ID"/>
<property name="Name" column="Name"/>
<!--additional properties-->
</class>
<class name="AttributeValue" table="AttributeValues" xmlns="urn:nhibernate-mapping-2.2">
<id name="Id" column="ID"/>
<many-to-one class="Attribute" column="ATTRIBUTE_ID"/>
<property name="Value" column="Value"/>
<!--additional properties-->
</class>

Map a column multiple times

I have a rather odd requirement in my fluent hibernate maps. I have an table(A) which has a compound foreign key relationship with another table(B). In the mapping for table A I would like to have both the object created from table B and access to the individual attributes of A which define the key. Is there any way to do that? I seem to get index out of range exceptions if I map the column twice.
I cannot just explore B for the attributes because the row in table B may not exist. I am painfully aware that there are some significant smells in the structure with which I'm dealing. Such is the fate of those who deal with legacy systems.
It's kinda possible, by hacking around a little.
We're going to define a domain that with a fake collection that we'll use to retrieve the single related element, if found:
public class Foo
{
public virtual BarKey BarKey { get; set; }
public virtual Bar Bar { get { return Bars.SingleOrDefault(); } }
protected virtual ICollection<Bar> Bars { get; set; }
}
public class Bar
{
public virtual BarKey Id { get; set; }
}
//this class must override Equals and GetHashcode. Implementation not shown.
public class BarKey
{
public virtual int X { get; set; }
public virtual int Y { get; set; }
}
The BarKey component contains the properties that are part of the key.
Now, the mapping:
<class name="Foo">
<id ...><generator .../></id>
<component name="BarKey">
<property name="X" />
<property name="Y" />
</component>
<bag name="Bars" inverse="true">
<key property-ref="BarKey">
<column name="X"/>
<column name="Y"/>
</key>
<one-to-many class="Bar"/>
</bag>
</class>
<class name="Bar">
<composite-id name="Id">
<key-property name="X" />
<key-property name="Y" />
</composite-id>
</class>
The property-ref attribute there tells NH to match those columns in Bar against the BarKey property of Foo instead of its Id.

Mapping for multi-interface-inherited class in NHibernate

I have interface:
public interface IHasList<T>
{
IList<T> Items { get; set; }
}
And I want to map such class using one-to-many mapping to the lists:
public class Model : IHasList<A>, IHasList<B>
{
...
}
Can I do this? If yes, how to write mapping?
It's possible, but a little weird.
First, in order to declare this in C#, Model would look like this:
public class Model : IHasList<A>, IHasList<B>
{
IList<A> IHasList<A>.Items { get; set; }
IList<B> IHasList<B>.Items { get; set; }
}
So you need to make NHibernate understand that:
<bag name="IHasList<A>.Items" table="ModelItemA">
<key />
<one-to-many class="A" />
</bag>
<bag name="IHasList<B>.Items" table="ModelItemB">
<key />
<one-to-many class="A" />
</bag>
(I'm assuming A and B are mapped entities with a regular one-to-many relationship, change that to many-to-many or element and add cascade/inverse attributes as needed)
It's pretty clean, the mess is actually introduced by XML escaping. You'll also have to use full names for the classes.

NHibernate.Linq - Custom/Calculated property expression

How can a domain object include a property that calculates a value from other database mapped properties so that the calculated property can be used in both the domain object instance and the db level by nhibernate.linq.
I would like to be able to use the property when working with the object directly:
Console.WriteLine(Entity.Calculated.ToString());
And when working with nhibernate.linq
var q = from e in session.Linq<Entity>()
where e.Calculated > 0
select e;
You need to duplicate the logic in the class and the mapping. Here's an example:
Class:
public class Invoice
{
public virtual int Id { get; protected set; }
public virtual decimal Amount { get; set; }
public virtual decimal Paid { get; set; }
public virtual decimal Balance
{
get { return Amount - Paid; }
}
}
Mapping:
<class name="Invoice">
<id name="Id">
<generator class="hilo"/>
</id>
<property name="Amount"/>
<property name="Paid"/>
<property name="Balance" formula="Amount - Paid" access="readonly"/>
</class>
Now you can use Linq and query on Invoice.Balance

How can I use a composite-id with a class as the id field in fluent nhibernate?

I've got a mapping file like this:
<class name="Resource" table="resource" discriminator-value="null">
<composite-id name="Key" class="Models.Bases.ClientKey, Models">
<key-property name="Id" column="ID" type="int"/>
<key-property name="SiteId" column="clientID" type="int"/>
</composite-id>
<property name="Name" type="String">
<column name="`name`" length="500" sql-type="varchar" not-null="false"/>
</property>
</class>
which works just fine and here's the id class:
public class ClientKey
{
public int Id { get; set; }
public int ClientId { get; set; }
}
public class Resource
{
public virtual ClientKey Key { get; set; }
public virtual string Name { get; set; }
}
How can I remap this using FluentNhibernate? This code doesn't work:
WithTable("resource");
UseCompositeId()
.WithKeyProperty(x => x.Key.Id, "ID")
.WithKeyProperty(x => x.Key.ClientId, "clientID");
Map(x => x.Name);
It throws this error:
Could not find a getter for property 'Id' in class 'Models.Resource'
Thanks!!!
I'm afraid you can't fix it without modifying Resource class. I have checked with Fluent NHibernate's source - here's what code that outputs composite-id part looks like:
XmlElement element = classElement.AddElement("composite-id");
foreach( var keyProp in keyProperties )
{
keyProp.Write(element, visitor);
}
What is missing is "name" attribute, which should be set to "Key". Without this attibute, NHibernate fallbacks to default property name = "Id". As your class doesn't have Id property, NHibernate doesn't know what to do and throws an exception.
If you can't modify Resource class, you would have to use hbm mapping for this class or create a patch to fluent nhibernate (it is possible that this is known issue and someone's working on it - refer to fluent nhibernate's issue tracker).