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
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 will force discriminator information to be included in queries.