Yes, table cars contain field idGarage, in which correct save id of Garage.
Code:
create table Bus (
idCar int null,
Color int null,
Number char(1) null,
id int identity,
constraint PK_BUS primary key (id)
)
create table Car (
idVehicle int null UNIQUE NONCLUSTERED,
Weight float null,
id int identity,
idGarage int null,
idDriver int null,
constraint PK_CAR primary key (id)
)
create table Driver (
idMan int null,
CarLicense int null,
Foto varbinary(100) null,
id int identity,
constraint PK_DRIVER primary key (id)
)
create table Garage (
CountCar int null,
id int identity,
constraint PK_GARAGE primary key (id)
)
create table Man (
Name varchar(10) null,
BirthDay datetime null,
id int identity,
constraint PK_MAN primary key (id)
)
create table Truck (
idCar int null UNIQUE NONCLUSTERED,
MaxCount int null,
id int identity,
constraint PK_TRUCK primary key (id)
)
create table Vehicle (
Name varchar(10) null,
Speed int null,
id int identity,
constraint PK_VEHICLE primary key (id)
)
alter table Bus
add constraint FK_BUS_GENERALIZ_CAR foreign key (idCar)
references Car (idVehicle)
alter table Car
add constraint FK_CAR_CAR_EXTEN_VEHICLE foreign key (idVehicle)
references Vehicle (id)
alter table Car
add constraint FK_CAR_CARS_GARAGE foreign key (idGarage)
references Garage (id)
alter table Car
add constraint FK_CAR__DRIVER_DRIVER foreign key (idDriver)
references Driver (id)
alter table Driver
add constraint FK_DRIVER_GENERALIZ_MAN foreign key (idMan)
references Man (id)
alter table Truck
add constraint FK_TRUCK_GENERALIZ_CAR foreign key (idCar)
references Car (idVehicle)
Why i should use bidirectional relationship? Car must not to know in which Garage it stay. May be here other reason to use bidirectional relationship?
I think trouble may be in inheritance. I.e. class Vehicle, class Car:Vehicle, class Bus:Car and class Truck:Car. Can I save in collection ISet Cars objects Car and his descendants?
When I retrieve object of type Car or Truck or Bus I get exception
An unhandled exception of type 'NHibernate.ADOException' occurred in nhibernate.dll
Additional information: Unable to perform find
When I retrieve Garage i get exception
An unhandled exception of type 'NHibernate.ADOException' occurred in nhibernate.dll
Additional information: could not initialize collection: [TestORM.Bll.Garage.Cars#1]
I'm entangled, because it's my first (test) nHibernate apllication.