views:

52

answers:

2

Here we go... I have a table (which unfortunatelly I can't change) with columns like: date, startTime, endTime. And I have data classes with two fields: startDateTime = date + startTime endDateTime = date + endTime

        Map(x => x.EndDateTime)
            .Columns.Clear()
            .Columns.Add("date", "endTime")
            .CustomType<MyCustomType>();

        Map(x => x.StartDateTime)
            .Columns.Clear()
            .Columns.Add("date", "startTime")
            .CustomType<MyCustomType>();

MyCustomType is a class implementing IUserType interface. This seemed to work, but it works only with reading data from database. While saving or updating NHibernate puts column "date" twice, and query can not be commited.

My question is: is any way to go around this? I want both fields to be not-read-only. (setting one of them as read-only helps, but it's not a solution which satisfies me).

A: 

Ok, i found a solution... it's not really elegant, but this database is not elegant either:)

So in my BaseController, which handles all stuff like adding, saving, deleting and updating i did some special handling of saving this particular type:

    public virtual T Save<T>(T entity)
    {
        try
        {
            if (entity.GetType().Equals(typeof(SpecialType)))
            {
                return SaveSpecialType<T>(entity);
            }
            ITransaction transaction = session.BeginTransaction();           
            session.Save(entity);
            transaction.Commit();
            return entity;
        }
        catch (Exception ex)
        {
            if (logError.IsErrorEnabled)
                logError.Error(ex.Message + " \n " + ex.InnerException, ex);
            throw;
        }
    }

This SaveSpecialType method executes native sql query inserting that messy type into database

Katalonis
A: 

Couple questions. What kind of errors do you get and what would you want to do if the two different properties have different values, even though they are supposed to go to the same underlying table field?

Rich
The error i get is:Parameter index is ut of range.Then, as for your secondquestion. In database I have three fields: Date (just date, as a string yyyyMMdd), startTime (as a string HHmm) and endTime (string HHmm).In data classes I have two fields in types of DateTime. SoStartDateTime = date + startTimeEndDateTme = date + endTimeThe problem is date is common for two fields. I hope i made myself clear:)
Katalonis
Instead of having two "unrelated" fields in the data object, can you live with having a single object (i.e. Period) which has properties Start/End. The I think you could map that one property to 3 columns in the database via a ICompositeUserType translator.
Rich
That would definitelly work, and thanks for help. Unfortunatelly I can't change anything. Neither Database, nor data classes. We're trying to do porting from old database (which is full of data) to new dataclasses (which are already in use with some clients, and which use new database). So it all should stay as it is... :)
Katalonis
OK, then what if you have 2 independent IUserTypes and use StartDateTime to define what the date and startTime field values are and use EndDateTime only to define what the endTime field is. You might also need to implement IParameterizedType and configure each property to know about the other (for instance, the EndDateTime will need to look at the StartDateTime for the date portion of the property value). I'm sure this isn't fully thought thru, but it might get you started.
Rich
This is quite the best solution to this problem I guess... Thanks for help!
Katalonis