views:

61

answers:

2

Hi all,

I have a class called WorkingDays, this class requires a collection of dates that are the dates of bank holidays. In our database we have a table that contains these dates - I want nhibernate to get all those dates and populate the bankHolidays collection.

The catch is however I do not have a table that represents the link in the database - it seems unsecessary really when all WorkingDays impls will have the exact same list of bank holiday dates.

So how can I map this without needing to add a foreign key?
I hope the code below can ilustrate better. Thanks for any help

public class WorkingDays : Interval
{
   public ICollection<DateTime> BankHolidays
   {
       get;
       private set;
   }
}
+1  A: 

Is there a reason you need this as a collection on the entity itself? Why not have a seperate repository/DAO responsible for accessing this collection? If the data were fairly static, you could probably gain quite a bit by using second level caching on this as well.

DanP
Im not sure how that will work unless i load the DateTimes and pass them to the WorkingDays - however because it inherits from Interval there would be no way to do this.
Gareth
I suppose that would depend on your usage; If you require this collection in downstream layers, you could always assign this property as part of a mapping operation, etc.
DanP
One more thought...what about a custom IPostLoadEventListener that populates the collection on the entity? I've never attempted anything like this, but I don't see why it wouldn't work.
DanP
Yes I was wondering about using a listener to achieve this - was hoping for a mapping file solution though. Thanks for the input
Gareth
A: 

If you only need a read-only collection, you could try the following:

<property name="BankHolidays" type="DateTime[]" formula="(SELECT Date FROM BankHolidays)" update="false" insert="false" />
JulianM
That sounds like the answer! Thanks - I'll try it monday
Gareth
Unfortunately it didn't work. When using the select statement in a query as suggested will only return the first DateTime found and not all of them
Gareth
Too bad, I was actually set to refactor some code if this panned out; I guess only scalar values are supported for formula-based properties.
DanP
Sorry for leading you up the garden path. Your requirement seems such a simple one that it is hard to believe NH doesn't have a simple mapping for it. You could try asking your question here: http://groups.google.com/group/nhusers
JulianM