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

The Archive Base Latest Questions

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

I am working on a very basic NHibernate 3.2 task, inserting records into existing

  • 0

I am working on a very basic NHibernate 3.2 task, inserting records into existing Postgres tables. I am using very simple object so that they make sense for this question.

The postgres tables are as follows:

CREATE TABLE cat
(
  id serial NOT NULL,
  "name" character varying(50) NOT NULL,
  sex_id integer NOT NULL,
  CONSTRAINT cat_pkey PRIMARY KEY (id),
  CONSTRAINT cat_sex_id_fkey FOREIGN KEY (sex_id)
      REFERENCES sex (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
  OIDS=FALSE
);

CREATE TABLE sex
(
  id serial NOT NULL,
  "name" character varying(10) NOT NULL,
  CONSTRAINT sex_pkey PRIMARY KEY (id),
  CONSTRAINT sex_name_key UNIQUE (name)
)
WITH (
  OIDS=FALSE
);

My mapping classes are as follows:

public class CatMap : ClassMapping<Cat>
    {
        public CatMap()
        {
            Table("cat");
            Id(x => x.Id, map =>
            {
                map.Column("id");
                map.Generator(NHibernate.Mapping.ByCode.Generators.Native);
            });
            Property(x => x.Name, map =>
            {
                map.Column("name");
                map.Length(50);
                map.NotNullable(true);
            });

            ManyToOne(x => x.Sex, map =>
            {
                map.Column("Sex");
                map.Unique(true);
                map.ForeignKey("cat_sex_id_fkey");
            });
        }
    }

public class SexMap : ClassMapping<Sex>
    {
        public SexMap()
        {
            Table("sex");
            Id(x => x.Id, map =>
                {
                    map.Column("id");
                    map.Generator(Generators.Native);
                });
            Property(x => x.Name, map => 
                {
                    map.Column("name");
                    map.Unique(true);
                    map.Length(10);
                    map.NotNullable(true);
                });
        }
    }

My data classes are as follows:

public class Sex
{
    public Sex() 
    {
    }
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
}

public class Cat
{
    public Cat() 
    {
    }
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual Sex Sex { get; set; }
}

Finally, the class containing the code where I am actually attempting to use all of the above.

public class Class1
    {
        public string DoSomething()
        {
            var sessionFactory = CreateSessionFactory();

            using (var session = sessionFactory.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    Postgres.Tables.Sex sex1 = new Postgres.Tables.Sex() { Name = "Male" };
                    Postgres.Tables.Sex sex2 = new Postgres.Tables.Sex() { Name = "Female" };

                    Postgres.Tables.Cat cat1 = new Postgres.Tables.Cat() { Name = "Cat1" };
                    cat1.Sex = sex1;
                    Postgres.Tables.Cat cat2 = new Postgres.Tables.Cat() { Name = "Cat2" };
                    cat2.Sex = sex2;

                    session.SaveOrUpdate(sex1);
                    session.SaveOrUpdate(sex2);
                    session.SaveOrUpdate(cat1);
                    session.SaveOrUpdate(cat2);

                    transaction.Commit();
                }
            }

            return "I created the cats.";
        }

        private static ISessionFactory CreateSessionFactory()
        {
            NHibernate.Mapping.ByCode.ModelMapper modelMapper = new NHibernate.Mapping.ByCode.ModelMapper();
            System.Type[] mappingTypes = typeof(Postgres.Tables.Mappings.CatMap).Assembly.GetExportedTypes().Where(t => t.Name.EndsWith("Map")).ToArray();
            modelMapper.AddMappings(mappingTypes);
            Configuration cfg = new Configuration();
            cfg.Proxy(p => p.ProxyFactoryFactory<NHibernate.Bytecode.DefaultProxyFactoryFactory>());
            cfg.DataBaseIntegration(d =>
                {
                    d.ConnectionString = "server=192.168.1.126;Port=5432;Database=simple;User Id=postgres;Password=postgres;";
                    d.Dialect<NHibernate.Dialect.PostgreSQL82Dialect>();
                });
            cfg.AddMapping(modelMapper.CompileMappingForAllExplicitlyAddedEntities());
            return cfg.BuildSessionFactory();
        }
    }

I receive a GenericADOException at “session.SaveOrUpdate(cat1)” with the message “could not insert: [DAL.Postgres.Tables.Cat][SQL: INSERT INTO cat (name, Sex) VALUES (?, ?); select lastval()]”. The InnerException is “{“ERROR: 42703: column \”sex\” of relation \”cat\” does not exist”}”.

I am a bit stumped at how to properly assign “sex1” to “cat1” and “sex2” to “cat2” so that the first is Male and the second is Female.

Thank you for any input.

  • 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-28T19:06:11+00:00Added an answer on May 28, 2026 at 7:06 pm

    m.Column(“Sex”) as the name suggests denote column name and not the property name as you specified. So you should write m.Column(“sex_id”). map.ForeignKey(“cat_sex_id_fkey”) is used by NH as a FK name when NH create db schema from your mapping.

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

Sidebar

Related Questions

I'm working on keyboard input for a very basic kernel that I'm developing and
I am working on a very large scale computing library that is using STL
So I'm using a very basic jQuery .slideDown which is working great in FF,
I need some help with something that is probably very basic. I'm working on
I've found a very basic modal window script that's working well on all browsers
Hey, I am working on a very basic parser. I am almost certain that
I'm working on a very basic shopping cart system. I have a table items
I have developed a simple location aware iPhone application which is functionally working very
I'm working on a very simple game (essentially an ice sliding puzzle), for now
I'm working on a very large project that has associated class files in multiple

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.