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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:02:10+00:00 2026-06-18T06:02:10+00:00

I need to set som default values and I have the following class and

  • 0

I need to set som default values and I have the following class and mapping respectively.

Farmacia.cs

public class Farmacia
{
    public virtual int Id { get; protected set; }
    public virtual string Nombre { get; set; }
    public virtual string Direccion { get; set; }
    public virtual string Telefono { get; set; }
    public virtual int CodigoPostal { get; set; }
    public virtual int Estado { get; set; }

    public Farmacia()
    {
    }
}

Farmacia.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                  assembly="Domain"
                  namespace="Domain">
  <class name="Farmacia" table="farmacias">
    <id name="Id" column="idFarmacia" type="int">
      <generator class="assigned" />
    </id>
    <property name="Nombre" />
    <property name="Direccion" />
    <property name="CodigoPostal">
      <column name="codPostal" default="1"/>
    </property>
    <property name="Telefono" />
    <property name ="Estado">
      <column name="estado" default="1" />
    </property>
  </class>
</hibernate-mapping>

Test

Domain.Farmacia f = new Domain.Farmacia { Nombre = "Test" };
Session.Save(f);
Session.Flush();

And I get a Exception that it cannot insert on database.

On the other hand, if I make some changes in the code (See below) I can save it.

public class Farmacia
{
    public virtual int Id { get; protected set; }
    public virtual string Nombre { get; set; }
    public virtual string Direccion { get; set; }
    public virtual string Telefono { get; set; }
    public virtual int CodigoPostal { get; set; }
    public virtual int Estado { get; set; }

    public Farmacia()
    {
        Estado = 1;          // New line.
        CodigoPosta = 1;     // New line.
    }
}

Farmacia.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                  assembly="Domain"
                  namespace="Domain">
  <class name="Farmacia" table="farmacias">
    <id name="Id" column="idFarmacia" type="int">
      <generator class="assigned" />
    </id>
    <property name="Nombre" />
    <property name="Direccion" />
    <property name="CodigoPostal" column="codPostal" />   // Property changed.
    <property name="Telefono" />
    <property name ="Estado" />                           // Property changed.
  </class>
</hibernate-mapping>

My problem is that I need the default values only if the object is new. If the object is already on the database, Always the objects from database will have some values in Estado and CodigoPostal.

P.D: I assigned default value in the database but the error persists.
So how can I set default values if the values are null (Object is new)?

  • 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-06-18T06:02:11+00:00Added an answer on June 18, 2026 at 6:02 am

    What you’ve done above by putting the default values in the constructor seems fine to me but if you’re looking for another way you might be able to use something like this:

    (7) dynamic-update (optional, defaults to false): Specifies that
    UPDATE SQL should be generated at runtime and contain only those
    columns whose values have changed.

    (8) dynamic-insert (optional, defaults to false): Specifies that
    INSERT SQL should be generated at runtime and contain only the columns
    whose values are not null.

    Above was taken from here: http://www.nhforge.org/doc/nh/en/index.html#mapping-declaration-class

    The only way you’d be able to use the database defaults though for integer columns would be to make them nullable and use dynamic-insert since the default value for an integer is 0.

    public class Farmacia
    {
        public virtual int Id { get; protected set; }
        public virtual string Nombre { get; set; }
        public virtual string Direccion { get; set; }
        public virtual string Telefono { get; set; }
        public virtual int? CodigoPostal { get; set; }
        public virtual int? Estado { get; set; }
    
        public Farmacia()
        {
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to return som values I have selected in a jQuery UI Dialog
I have the following dependencies that need set up at that start of my
I have the following need I have a logging table which logs som leads
First, I have an object like that: public class Entity { public int data1;
I need Set collection, where its items will be identified by items class. Something
I need to set the default icon that VS uses for the project to
Me need set item checkbox when i click on element span which have this
1) i need set different class name for every menu item, based on a
I need to set different styles for cells that I have inserted compared to
I need set default selected value with text, here is my code: HTML: <select

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.