Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 70611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:45:29+00:00 2026-05-10T19:45:29+00:00

I have a Oracle database and one of the fields is a date range

  • 0

I have a Oracle database and one of the fields is a date range field. It is basically just stored in the database as a VARCHAR(40) in the format YYYY/MM/DD-YYYY/MM/DD. I want to map it in nHibernate to a custom class I have created like this

public class DateTimeRange {     public DateTimeRange(DateTime fromTime, DateTime toTime)     {         FromTime = fromTime;         ToTime = toTime;     }      public override string ToString()     {         return String.Format('{0} to {1}', FromTime.ToString('HH:mm:ss'), ToTime.ToString('HH:mm:ss'));     }      public DateTime FromTime { get; set; }      public DateTime ToTime { get; set; } } 

How can I map to custom classes like this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T19:45:29+00:00Added an answer on May 10, 2026 at 7:45 pm

    You need to implement your own IUserType.

    See this blog post for details. I’ll also paste the relevant section below in case the blog disappears.

    In NHibernate, a custom mapping type is a class that derives from either the IUserType or ICompositeUserType interfaces. These interfaces contain several methods that must be implemented, but for our purposes here, we’re going to focus on 2 of them. Consider the following.

      public class TypeClassUserType : IUserType   {       object IUserType.NullSafeGet(IDataReader rs,        string[] names,       object owner) {       string name = NHibernateUtil.String.NullSafeGet(rs,       names[0]) as string;       TypeClassFactory factory = new TypeClassFactory();      TypeClass typeobj = factory.GetTypeClass(name);      return typeobj;    }      void IUserType.NullSafeSet(IDbCommand cmd,      object value,       int index) {        string name = ((TypeClass)value).Name;      NHibernateUtil.String.NullSafeSet(cmd, name, index);     }   } 

    Having created this class, I can now explicitly map the association between ActualClass and TypeClass as a simple property on the ActualClass mapping.

    <property   name='Type'   column='TypeName'   type='Samples.NHibernate.DataAccess.TypeClassUserType,          Samples.NHibernate.DataAccess' /> 

    As NHibernate is in the process of saving an instance of ActualType, it will load and create a new instance of TypeClassUserType and call the NullSafeSet method. As you can see from the method body, I am simply extracting the name from the mapped property (passed in as the value parameter) and setting the extracted name as the value of the parameter to be set in the database. The net result is that although the Type property of ActualClass is TypeClass in the domain model, only the Name property of the TypeClass object gets stored in the database. The converse is also true. When NHibernate is loading an instance of ActualType from the database and the finds a property of my custom mapping type, it loads my custom type and calls the NullSafeGet method. As you can see, my method gets the name from the returned data, calls my flyweight factory to get the correct instance of TypeClass, and then actually returns that instance. The type resolution process happens transparently to my data access classes (and even to NHibernate itself for that matter).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 123k
  • Answers 123k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Your approach is perfectly valid (though I would use a… May 12, 2026 at 1:04 am
  • Editorial Team
    Editorial Team added an answer Check whether your PHP configuration has magic_quotes_gpc activated, in such… May 12, 2026 at 1:04 am
  • Editorial Team
    Editorial Team added an answer If it feels wrong because events are more sophisticated and… May 12, 2026 at 1:04 am

Related Questions

I have a particular PHP page that, for various reasons, needs to save ~200
I am retrieving data from an Oracle database and binding the same to a
I am interested in what methods of logging is frequent in an Oracle database.
I'm new to the whole RFID arena. I need to store an RFID pr

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.