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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:15:22+00:00 2026-05-24T10:15:22+00:00

I have a pretty simple problem, but right now can’t seem to see the

  • 0

I have a pretty simple problem, but right now can’t seem to see the point.
My intention is to read a List of VehicleCollector objects, each one with a neat list of Cars and another list of Trains. Vehicles come from a single table and are discriminated by a column named TYPE.

Model Code:

public class VehicleCollector
{
    public virtual IList<Car> Cars { get; set; }
    public virtual IList<Train> Trains { get; set; }
}

public class Vehicle { }

public class Car : Vehicle {}

public class Train : Vehicle { }

Mapping Code:

public class FooMap : ClassMap<VehicleCollector>
{
    public FooMap()
    {
        this.HasMany(x => x.Cars).KeyColumn("foo_id");
        this.HasMany(x => x.Trains).KeyColumn("foo_id");
    }
}

public class VehicleMap : ClassMap<Vehicle>
{
    public VehicleMap() { this.DiscriminateSubClassesOnColumn("type"); }
}

public class CarMap : SubclassMap<Car>
{
    public CarMap() { this.DiscriminatorValue(1); }
}

public class TrainMap : SubclassMap<Train>
{
    public TrainMap() { this.DiscriminatorValue(2); }
}

I left out IDs and other properties intentionally to keep this readable. It won’t compile if you run it the way it is.

What actually happens is that all vehicles are loaded into either one of the collections, so – to my understanding – the discriminator has been neglected.

Do you have any idea what I might be missing / doing wrong here?

EDIT: I just ran a SchemaExport to SQLite to see how this behaves. Here’s some more detail on what’s happening – step by step. Note : Some properties may have changed / been added.

1) SchemaExport

 create table vehicle_collector
 (
    id INTEGER not null,
   name TEXT not null,
   primary key (id)
);

create table vehicle (
    id INTEGER not null,
   desc TEXT not null,
   type TEXT not null,
   collector_id INTEGER,
   primary key (id)
);

2) Inserting

VehicleCollector c = new VehicleCollector() { Id = 1001, Name = "foobar" };
Train v2 = new Train() { Id = 101, desc = "Foo" };
Car v1 = new Car() { Id = 102, desc = "Bar" };
c.Cars.Add(v1);
c.Trains.Add(v2);
openSession.Save(c);

This results in the following SQL (Note that the discriminators are placed as static strings by NHibernate):

INSERT INTO vehicle_collector (name, id) VALUES (@p0, @p1); @p0 = 'foobar', @p1 = 1001;
INSERT INTO vehicle (desc, type, id) VALUES ( @p1, '2', @p2); @p1 = 'Foo', @p2 = 101;
INSERT INTO vehicle (desc, type, id) VALUES ( @p1, '1', @p2); @p1 = 'Bar', @p2 = 102;
UPDATE vehicle SET collector_id = @p0 WHERE id = @p1; @p0 = 1001, @p1 = 101;
UPDATE vehicle SET collector_id = @p0 WHERE id = @p1; @p0 = 1001, @p1 = 102;

3) Selecting

VehicleCollector v1 = openSession.CreateCriteria<VehicleCollector>()
                                    .Add(Restrictions.Eq("Id", 1001L))
                                    .SetMaxResults(1)
                                    .List<VehicleCollector>()
                                    .First();
Assert.AreEqual(1, v1.Cars.Count);
Assert.AreEqual(1, v1.Trains.Count);

This results in the following SQL (Why are there no discriminators here? And what is @p1 in the first select statement generated for?):

 SELECT this_.id as id0_0_, this_.name as name0_0_
   FROM vehicle_collector this_
  WHERE this_.id = @p0 limit 1;
 @p0 = 1001, @p1 = 1;

 SELECT vehicle0_.collector_id as collector5_1_,
        vehicle0_.id        as id1_,
        vehicle0_.id        as id1_0_,
        vehicle0_.desc      as desc1_0_
   FROM vehicle vehicle0_
  WHERE vehicle0_.collector_id = @p0;
 @p0 = 1001;

Obivously, NHibernate 2.1 has trouble selecting using the discriminator, while inserting works as intended. Any fresh ideas?

TIA

  • 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-24T10:15:23+00:00Added an answer on May 24, 2026 at 10:15 am

    I’ve had something similar (if not the same) where selecting things wasn’t working because it wasn’t including any of the discriminator information. To force discriminator information to ALWAYS be included, add this:

    this.DiscriminateSubClassesOnColumn("type").AlwaysSelectWithValue();
    

    This will force discriminator information to be included in queries.

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

Sidebar

Related Questions

I am pretty new to php, but I am learning! I have a simple
I have a pretty complicated Linq query that I can't seem to get into
To be honest, I'm feeling pretty stupid right now. But this simply isn't working
Should be pretty simple: I have an InputStream where I want to peek at
Pretty simple question: When i have a persistable object, it usually has a property
Pretty simple scenario. I have a web service that receives a byte array that
I have pretty much finished my first working Symbian application, but in my hastened
I'm don't use PHP that much and right now I'm stuck at a problem.
I have a pretty simple table in SQLite, with the following schema: CREATE TABLE
I'm having this problem right now: given a month and a year, I'd need

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.