Trying to run query for entities when I have Inheritance with fluent Nhibernate - nhibernate

I attempted to extract some common properties to a base class and map with Fluent Nhibernate. In addition, I also attempted to add a second level of inheritance.
//Base entity class
public class EntityBase : IEntityBase
{
public EntityBase()
{
CreatedDate = DateTime.Now;
}
public virtual DateTime? CreatedDate { get; set; }
public virtual int Id { get; set; }
public virtual int Version { get; set; }
}
//Base Entity Mapping
public class EntityBaseMap: ClassMap<EntityBase>
{
public EntityBaseMap()
{
UseUnionSubclassForInheritanceMapping();
Id(x => x.Id);
Version(x => x.Id);
Map(x => x.CreatedDate);
}
}
//first sub class of EntityBase
public class Actuate : EntityBase, IActuate
{
public virtual DateTime? ActivatedOn { get; set; }
}
//Actuate Mapping class
public class ActuateMap : SubclassMap<Actuate>
{
public ActuateMap()
{
Map(x => x.ActivatedOn);
}
}
//Sub class entity
public class Item : Actuate
{
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual decimal UnitPrice { get; set; }
public virtual ItemStatus Status { get; set; }
public virtual Store Store { get; set; }
}
//Item Mapping class
public class ItemMap : SubclassMap<Item>
{
public ItemMap()
{
Abstract();
Map(x => x.Name);
Map(x => x.Description);
Map(x => x.UnitPrice);
Map(x => x.Status);
References(x => x.Store);
}
}
The entity I have discovered has a problem (other relationship issues might exists)
//Store entity Does not inherit from EntityBase or Actuate
public class Store
{
public virtual int Id { get; set; }
public virtual int Version { get; set; }
public virtual string Name { get; set; }
public virtual IEnumerable<Item> Items { get; set; }
}
//Store mapping class
public class StoreMap : ClassMap<Store>
{
public StoreMap()
{
Id(x => x.Id).GeneratedBy.Assigned();
Version(x => x.Version);
Map(x => x.Name);
HasMany(x => x.Items);
}
}
Problem
If I try to run the following query:
//store = is the Store entity I have retrieved from the database and I am trying
//trying to return the items that are associated with the store and are active
store.Items != null && store.Items.Any(item => item.Status == ItemStatus.Active);
I get the following error:
ERROR
Nhibernate.Exceptions.GenericADOException: could not initialize a collection: [SomeDomain.Store.Items#0][SQL: SELECT items0_.StoreId as StoreId1_, items0_.Id as Id1_, items0_.Id as Id10_0_, items0_.CreatedDate as CreatedD2_10_0_, items0_.ActivatedOn as Activate1_11_0_, items0_.Name as Name12_0_, items0_.Description as Descript2_12_0_, items0_.UnitPrice as UnitPrice12_0_, items0_.Status as Status12_0_, items0_.StoreId as StoreId12_0_ FROM [Item] items0_ WHERE items0_.StoreId=?]"}
Inner Exception
"Invalid object name 'Item'."
Now, if I take out the base classes and Item doesn't inherit, and the
Id, Version
columns are part of the Item entity and are mapped in the ItemMap mapping class (with the ItemMap class inheriting from ClassMap<Item> instead, everything works without issue.
NOTE
I have also attempted to add on the StoreMap class unsuccessful.
HasMany(x => x.Items).KeyColumn("Id");
Any thoughts?

if entityBase is just for inheriting common properties then you do not need to map it at all
public class EntityBaseMap<TEntity> : ClassMap<TEntity> where TEntity : EntityBase
{
public EntityBaseMap()
{
Id(x => x.Id);
Version(x => x.Version);
Map(x => x.CreatedDate);
}
}
public class ActuateMap : EntityBaseMap<Actuate> { ... }
Notes:
Versionmapping should map Version property not Id
Version should be readonly in code so nobody accidently alters it
.KeyColumn("Id") is wrong because the column is from the Items table and then it's both the autogenerated id and foreign key. That's not possible nor usefull
usually only classes which are abstract should containt Abstract() in the mapping

Related

Nhibernate one to many relationship composite key issue

Below is my Parent child relationship class and mapper. We are using Nhibernate 4.0.0.4000 in our project. When I call session.Merge(Parent) to update the parent which has a new child object to insert into Db. It throws cannot insert Null exception for code_column in child object. Can somebody guide which part in my mapper code is wrong?
Public class parent {
public virtual string Code { get; set; }
public virtual string Desc { get; set; }
public virtual IList<Child> Children{ get; set; }
public virtual int version {get;set;}
}
Public class Child {
public virtual parent ParentObj{ get; set; }
public virtual string Code1{ get; set; }
public virtual string Code2{ get; set; }
public virtual int version {get;set;}
}
public class ParentMap : ClassMap<Parent> {
public ParentMap () {
Table("Parent_Table");
LazyLoad();
OptimisticLock.Version();
Id(x => x.Code )
.Column("Code_Column")
.Index("Code_IDX1")
.Length(5)
.Unique()
.GeneratedBy.Assigned()
.Not.Nullable();
Version(x => x.Version)
.Column("VERS")
.UnsavedValue("0");
HasMany(x => x.Children)
.AsBag()
.KeyColumn("Code_Column")
.Inverse()
.LazyLoad()
.Cascade.All();
}
}
public class ChildMap: ClassMap<Child> {
public ChildMap() {
Table("Child_Table");
LazyLoad();
OptimisticLock.Version();
CompositeId()
.KeyReference(u => u.Code, "Code_Column")
.KeyProperty(u => u.Code1, "CODE1_column")
.KeyProperty(u => u.Code2, "CODE2_column");
Version(x => x.Version)
.Column("VERS")
.UnsavedValue("0");
}
}
It was the composite Id which had the issue with session.merge method probably because of the lazy load. When you have composite keys, Create a class for the composite keys and use them in your entity which would work fine.

Unable to cast object of type 'NHibernate.Mapping.List' to type 'NHibernate.Mapping.IKeyValue'

Trying to map a relationship in Fluent NHibernate and I'm getting this exception.
Here are the classes
public abstract class TaskContainer : DomainObject
{
public virtual IList<Task> Tasks { get; set; }
}
public class Task : TaskContainer
{
public virtual TaskContainer TaskContainer { get; set; }
public virtual string Description { get; set; }
public static Task Get(int id)
{
return Get<Task>(id);
}
}
And the mapping file
public class TaskMap : ClassMap<Task>
{
public TaskMap()
{
Id(x => x.Id);
Map(x => x.Description);
ReferencesAny(x => x.TaskContainer)
.EntityIdentifierColumn("TaskContainer_Id")
.EntityTypeColumn("TaskContainerDiscriminator")
.IdentityType<int>()
.AddMetaValue<Task>("Task")
HasMany(x => x.Tasks)
.KeyColumn("TaskContainer_Id")
.PropertyRef("Tasks")
.AsList();
}
}
I've seen other references to this error, but they had to do with implementing List<T> instead of IList<T>, which I am not doing.
Try: .PropertyRef("TaskContainer")

Fluent NHibernate: map list of abstract class with table per concrete class (union-subclass)

I'm having a problem with the following scenario.
My class structure is as follows:
public class Owner
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<Vehicle> Vehicles { get; set; }
}
public abstract class Vehicle
{
public virtual long Id { get; set; }
public virtual string Name { get; set; }
}
public abstract class PoweredVehicle : Vehicle
{
public virtual string EngineType { get; set; }
}
public class Car : PoweredVehicle
{
public virtual int Doors { get; set; }
}
public class Truck : PoweredVehicle
{
public virtual long MaxLoad { get; set; }
}
public class Bicycle : Vehicle
{
public virtual int FrameSize { get; set; }
}
Fluent mappings:
public class OwnerMap : ClassMap<Owner>
{
public OwnerMap()
{
Id(x => x.Id).GeneratedBy.GuidComb();
Map(x => x.Name);
HasMany(x => x.Vehicles);
}
}
public class VehicleMap : ClassMap<Vehicle>
{
public VehicleMap()
{
Id(x => x.Id).GeneratedBy.HiLo("10");
Map(x => x.Name);
UseUnionSubclassForInheritanceMapping();
}
}
public class PoweredVehicleMap : SubclassMap<PoweredVehicle>
{
public PoweredVehicleMap()
{
Map(x => x.EngineType);
Abstract();
}
}
public class CarMap : SubclassMap<Car>
{
public CarMap()
{
Map(x => x.Doors);
}
}
public class TruckMap : SubclassMap<Truck>
{
public TruckMap()
{
Map(x => x.MaxLoad);
}
}
public class BicycleMap : SubclassMap<Bicycle>
{
public BicycleMap()
{
Map(x => x.FrameSize);
}
}
I insert a Car and a Bicycle. When I try to insert an Owner with a list of Vehicle objects (with a Car and a Bicycle), I get the following error:
Exception: NHibernate.Exceptions.GenericADOException: could not insert
collection:
[NHibernateTest.Owner.Vehicles#8ace95bc-ad80-46d7-94c7-a11f012b67c6][SQL:
UPDATE "Vehicle" SET Owner_id = #p0 WHERE Id = #p1] --->
System.Data.SQLite.SQLiteException: SQLite error
Since I setup table per concrete class, why is NHibernate trying to update a non-existing table, which is representing the base class? Is this type of mapping not supported for this scenario?
Also, when I change from HasMany to HasManyToMany, this works fine.
In this case the only choice is Inverse() mapping. This means that the concrete Vehicle (Car, Bicycle) must care about the persistence of the relationship.
To enable this, extend the Vehicle class with new property:
public abstract class Vehicle
{
..
// new owner independent navigation property
public virtual Guid OwnerId { get; set; }
}
and extend mapping of the Vehicle
public VehicleMap()
{
..
Map(x => x.OwnerId).Column("Owner_id);
}
and finally invert persistence responsibility. Not the owner, but the collection item will care about correct Owner_id column changes (when concrete Vehicle insert/update is invoked).
(more about inverse: https://stackoverflow.com/a/1454445/1679310)
public OwnerMap()
{
..
HasMany(x => x.Vehicles)
.Inverse()
.Cascade.All();
}
When Vehicle is added into Owner's collection, its OwnerId must be also assigned:
owner.Vehicles.Add(car);
car.OwnerId = owner.Id;

Fluent mapping base class - Tried to add property when already added

I have a base class that looks like this:
public abstract class MyBaseClass
{
public virtual DateTime UpdatedOn { get; set; }
}
I then have a series of other entities that inherit from this:
public class User : MyBaseClass
{
public virtual string UserName { get; set; }
public virtual string Password { get; set; }
}
My mapping for User would be:
public class UserMap : ClassMap<User>
{
public UserMap()
{
Id(x => x.UserName);
Map(x => x.Password);
Map(x => x.UpdatedOn);
}
}
I then have other entities, mapped the same way.
My problem is I get the following error:
Tried to add property 'UpdatedOn' when already added.
I guess this is because I map the UpdatedOn column in every entity?
Each of my tables has this UpdatedOn column, so how should I be mapping it?
Use a derived class in fluent nHibernate
This question should help you out. You can basically use a base fluent nhibernate mapping class to map your UpdatedOn column and derive all of your other mapping classes from that base class.
I had similar kind of situation. I did like this..it works...
I was supposed to exposes entities as Interface..
public interface IEntity
{
DateTime CreationDate { get; set; }
DateTime UpdationDate { get; set; }
}
public interface IUser : IEntity
{
DateTime UserName { get; set; }
DateTime Password { get; set; }
}
public interface IEmployee : IEntity
{
DateTime Name { get; set; }
DateTime Key { get; set; }
}
public abstract class Entity : IEntity
{
public virtual DateTime CreationDate { get; set; }
public virtual DateTime UpdationDate { get; set; }
}
public class User : Entity, IUser
{
public virtual string UserName { get; set; }
public virtual string Password { get; set; }
}
public class Employee : Entity, IEmployee
{
public virtual string Name { get; set; }
public virtual string Key { get; set; }
}
public class UserMap : ClassMap<User>
{
public UserMap()
{
Id(x => x.UserName);
Map(x => x.Password);
Map(x => x.CreationDate);
Map(x => x.UpdationDate);
}
}
public class EmployeeMap : ClassMap<Employee>
{
public EmployeeMap()
{
Id(x => x.Name);
Map(x => x.Key);
Map(x => x.UpdationDate);
Map(x => x.CreationDate);
}
}

Simple mapping in fluent nhibernate

I have a class Client which has a attribute of dogs
public class ClientsMap : ClassMap<Clients>
{
public ClientsMap()
{
Id(x => x.ClientID);
HasMany(x => x.Dogs);
}
}
public class Client
{
public virtual IList<Dog> Dogs { get; set; }
public virtual int ClientID { get; set; }
}
and a class of dog that references client.
public class Dog
{
public virtual Clients Client { get; private set; }
public virtual int Id { get; set; }
}
public class DogMap : ClassMap<Dog>
{
public DogMap()
{
Table("Pooches");
Id(x => x.Id);
References(x => x.Client).Column("ClientId");
}
}
Because I am mapping on to an existing DB i cannot change the field names.
When I try and return the dogs collection I am getting an invalid column error on client_id with the SQL
SELECT
dogs0_.Clients_id as Clients3_1_,
dogs0_.Id as Id1_,
dogs0_.Id as Id1_0_,
dogs0_.ClientId as ClientId1_0_
FROM
pooches dogs0_
How can I make this use clientid over cliet_id. I thought I specified this in the dogs map.
You should also specify the column name on the one to many relationship.
HasMany(x => x.Dogs)
.KeyColumn("ClientId");