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 7183793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:04:04+00:00 2026-05-28T18:04:04+00:00

Is it possible to map a database column to a constant value without the

  • 0

Is it possible to map a database column to a constant value without the need for a property in the entity class? This basically is a workaround for a missing default value on that column in the database in combination with a NOT NULL constrained. The database is external and can’t be changed but I don’t need all of the columns in that table and thus don’t want to have corresponding properties in my entity class.

I am asking basically the same as described in this Hibernate JIRA issue.

  • 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. Editorial Team
    Editorial Team
    2026-05-28T18:04:06+00:00Added an answer on May 28, 2026 at 6:04 pm

    My implementation takes the same idea as hival but goes a lot further. the basis is an implementation of IPropertyAccessor

    /// <summary>
    /// Defaultvalues für nicht (mehr) benötigte Spalten siehe
    /// http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/
    /// </summary>
    public abstract class DefaultValuesBase : IPropertyAccessor
    {
        public abstract IEnumerable<IGetter> DefaultValueGetters { get; }
    
        public bool CanAccessThroughReflectionOptimizer
        {
            get { return false; }
        }
    
        public IGetter GetGetter(Type theClass, string propertyName)
        {
            return DefaultValueGetters.SingleOrDefault(getter => getter.PropertyName == propertyName);
        }
    
        public ISetter GetSetter(Type theClass, string propertyName)
        {
            return new NoopSetter();
        }
    }
    
    // taken from the link
    [Serializable]
    public class DefaultValueGetter<T> : IGetter {...}
    
    // ---- and the most tricky part ----
    public static void DefaultValues<T>(this ClasslikeMapBase<T> map, DefaultValuesBase defaults)
    {
        DefaultValuesInternal<T>(map.Map, defaults);
    }
    
    public static void DefaultValues<T>(this CompositeElementPart<T> map, DefaultValuesBase defaults)
    {
        DefaultValuesInternal<T>(map.Map, defaults);
    }
    
    private static void DefaultValuesInternal<T>(
        Func<Expression<Func<T, object>>, PropertyPart> mapFunction, DefaultValuesBase defaults)
    {
        var noopSetter = new NoopSetter();
        var defaultsType = defaults.GetType();
    
        foreach (var defaultgetter in defaults.DefaultValueGetters)
        {
            var parameter = Expression.Parameter(typeof(T), "x");
            Expression body = Expression.Property(parameter,
                new GetterSetterPropertyInfo(typeof(T), defaultgetter, noopSetter));
    
            body = Expression.Convert(body, typeof(object));
    
            var lambda = Expression.Lambda<Func<T, object>>(body, parameter);
    
            mapFunction(lambda).Column(defaultgetter.PropertyName).Access.Using(defaultsType);
        }
    }
    
    // GetterSetterPropertyInfo inherits PropertyInfo with important part
    public override string Name
    {
        get { return m_getter.PropertyName; } // propertyName is the column in db
    }
    
    // and finally in SomeEntityMap
    this.DefaultValues(new SomeEntityDefaults());
    
    public class SomeEntityDefaults : DefaultValuesBase
    {
        public override IEnumerable<IGetter> DefaultValueGetters
        {
            get
            {
                return new [] {
                    new DefaultValueGetter<int>("someColumn", 1),
                    new DefaultValueGetter<string>("somestrColumn", "empty"),
                };
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to map an entity to a database view using code-first?
I'd like to know if it is possible to map some database columns to
I'm attempting to map a database field (LS_RECNUM) possible values of NULL, 'M' and
Is this possible to map a SQL Native query (instead of a table) with
I am looking to take a map<char, vector<char> > and generate each possible map<char,
Is it possible to map an enum as a string using Fluent Nhibernate?
Is it possible to map a route with MapRoute and specify a generic controller
Is it possible to map an array to a new array and to sort
I'm wondering, is it possible to somehow map a key-press event to act like
Possible Duplicate: What does map(&:name) mean in Ruby? I was watching a railscast and

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.