NHibernate: Persisting subclassed entity to main table (table per class strategy issue) - nhibernate

It is not a question but just for future... (spent many time to figure out)
public class A
{
public int Id { get; set; }
public string Name { get; set; }
public string Type { get; set; }
}
public class B : A
{
public string Lastname { get; set; }
}
Mappings (Fluent NHibernate):
public class AMap : ClassMap<A>
{
Table("a");
Id(x => x.Id).GeneratedBy.Native();
Map(x => x.Name);
Map(x => x.Type);
DiscriminateSubClassesOnColumn<string>("Type")
.AlwaysSelectWithValue();
}
public class BMap : ClassMap<B>
{
DiscriminatorValue("BType");
Extends<A>();
Map(x => x.Lastname);
}
Persisting BMap object leads to Exception:
Exception:
Error dehydrating property value for A.Lastname
Inner Exception:
Invalid index 1 for this SqlParameterCollection with Count=1
This unobvious exceptions tell that some mapping missconfiguration occured. Actually "select" queries work pretty fine. But "insert into" (ISession.SaveOrUpdate) causes mentioned errors.
FIX:
In AMap class you don't need that line:
Map(x => x.Type);
After removing this persister works fine

type does not need to be in the class nor mapped as property, it is purly implementation detail in the database to discriminate the types. Instead of Extends SubclassMap is more appropriate:
public class AMap : ClassMap<A>
{
Table("A");
Id(x => x.Id).GeneratedBy.Native();
Map(x => x.Name);
DiscriminateSubClassesOnColumn<string>("Type", "AType")
.AlwaysSelectWithValue();
}
public class BMap : SubclassMap<B>
{
DiscriminatorValue("BType");
Map(x => x.Lastname);
}

Related

Table-per-subclass fluent nhibernate not working

I have the following classes defined:
And these tables in my database:
My fluent NHibernate mappings are:
public class BusinessUnitMap : ClassMap<BusinessUnit>
{
public BusinessUnitMap()
{
Table("BusinessUnits");
Id(x => x.Id);
Map(x => x.Code);
Map(x => x.Name);
Map(x => x.ParentId);
Map(x => x.Type).Column("Type").CustomType<BusinessUnitType>();
}
}
public class CompanyMap : SubclassMap<Company>
{
public CompanyMap()
{
Table("CompanyData");
KeyColumn("BusinessUnitID");
Map(x => x.Something);
}
}
public class FranchiseeMap : SubclassMap<Franchisee>
{
public FranchiseeMap()
{
Table("FranchiseeData");
KeyColumn("BusinessUnitID");
Map(x => x.SomethingDifferent);
}
}
public class StoreMap : SubclassMap<Store>
{
public StoreMap()
{
Table("StoreData");
KeyColumn("BusinessUnitID");
Map(x => x.SomethingElse);
}
}
Question #1
As far as I can tell, my code and database are setup the same as every example I've been able to find. According to those articles, NHibernate is supposed to be smart enough to determine what subclass to instantiate when I query for a particular subclass. But, when I execute the following statement:
var result = Session.QueryOver<BusinessUnit>()
.Where(x => x.Code == "Acme")
.SingleOrDefault();
an exception is thrown because it can't create an instance of the abstract BusinessUnit class. The only way I can get this to work is to specify Company as the type argument for QueryOver.
I've confirmed that using a discriminator breaks since NHibernate is looking for all of the columns to exist in a single table. Without it, though, I struggle to see how NHibernate would know what type to instantiate.
What am I doing wrong? Is the problem in my mappings, the way I'm querying, ...?
Question #2
When I change the query to something like this:
public T WithCode<T>(String code)
where T : BusinessUnit
{
var result = Session.QueryOver<T>()
.Where(x => x.Code == code)
.SingleOrDefault();
return result;
}
I get an exception indicating that the UPDATE statement conflicts with a foreign key constraint. Update statement!!!! Clearly something is still not right. How can a QueryOver call result in an UPDATE statement? What am I missing?
it looks like your data is not consistent. It might be better to use discrimnator mapping with optional. If you dont really need a BusinessUnitType property in code then just delete everything around the property Type
public enum BusinessUnitType
{
Company,
Franchisee
}
public abstract class BusinessUnit
{
public virtual int Id { get; set; }
public virtual string Code { get; set; }
public virtual string Name { get; set; }
public virtual BusinessUnit Parent { get; set; }
public abstract BusinessUnitType Type { get; }
}
public class Company : BusinessUnit
{
public virtual string Something { get; set; }
public override BusinessUnitType Type { get { return BusinessUnitType.Company; } }
}
public class Franchisee : BusinessUnit
{
public virtual string SomethingDifferent { get; set; }
public override BusinessUnitType Type { get { return BusinessUnitType.Franchisee; } }
}
public class BusinessUnitMap : ClassMap<BusinessUnit>
{
public BusinessUnitMap()
{
Table("BusinessUnits");
Id(x => x.Id);
Map(x => x.Code);
Map(x => x.Name);
References(x => x.Parent);
DiscriminateSubClassesOnColumn("Type");
Map(x => x.Type, "Type")
.Access.None()
.CustomType<BusinessUnitType>().ReadOnly();
}
}
public class CompanyMap : SubclassMap<StrangeTablePerSubclass.Company>
{
public CompanyMap()
{
DiscriminatorValue((int)new Company().Type);
Join("CompanyData", join =>
{
join.KeyColumn("BusinessUnitID");
join.Optional();
join.Map(x => x.Something);
});
}
}
public class FranchiseeMap : SubclassMap<Franchisee>
{
public FranchiseeMap()
{
DiscriminatorValue((int)new Franchisee().Type);
Join("FranchiseeData", join =>
{
join.KeyColumn("BusinessUnitID");
join.Optional();
join.Map(x => x.SomethingDifferent);
});
}
}

Trying to run query for entities when I have Inheritance with fluent 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

How do I store a Serializable List<int> in FluentNhibernate

If I have the following (cut down for brevity)
public class TempCartMap : ClassMap<TempCart>
{
Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.Products); // < This one
}
[Serializable]
public class TempCart {
public TempCart(){
Products = new List<int>();
}
public virtual IList<int> Products { get; set; }
}
I've looked at (http://www.philliphaydon.com/2012/06/using-nhibernate-with-servicestack/) and (http://www.philliphaydon.com/2012/03/ormlite-blobbing-done-with-nhibernate-and-serialized-json/) but is there a shorter, simpler, faster way of getting NHibernate to Serialize & De-serialize a column as above.
Seems overkill to create a new IUserType class etc etc.
You can also store the Products-list as a separate table:
public class TempCartMap : ClassMap<TempCart>
{
Id(x => x.Id).GeneratedBy.Identity();
HasMany(x => x.Products)
.Element("product_number")
.KeyColumn("temp_cart_id")
.Table("temp_cart_products")
;
}
Just to clarify the question: is there a reason why this is not wanted?

"Tried to add property 'Id' when already added."-Error when implementing own class twice

I would like to have a mapped class named "Message". This class should include an unique id, the title, the text, and information about the sender and the receiver. I need their User-ID an their name, so I've created another class named "User". This class include these two properties (later I'll create some methods for this class and use it in different classes, so I can not use onyl the class "Message").
This is my code
public class User
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
}
public class Message
{
public virtual Guid Id { get; set; }
public User sender;
public User receiver;
public virtual string subject { get; set; }
public virtual string text { get; set; }
}
public class MessageMap : ClassMap<Message>, IMappedEntity
{
public MessageMap()
{
Id(x => x.Id, "MessageId");
Map(x => x.sender.Id, "SenderId");
Map(x => x.receiver.Id, "ReceiverId");
Map(x => x.subject);
Map(x => x.text);
}
}
As you can see, I want to save only the User-ID of the sender and receiver, not their names. Because x.Id, x.sender.Id and x.receiver.Id have the property "Id", I wrote down a spezific name for them in the database.
But if I try to load the site, this error appears: Tried to add property 'Id' when already added., even if their is no more property named "Id" after I definited the Name for the columns...
Could you give me a hint what I'm doing wrong?
I finally found an other solution: I changed the mapping of my Message-Map to this:
public class MessageMap : ClassMap<Message>, IMappedEntity
{
public MessageMap()
{
Id(x => x.Id);
Component(
x => x.sender,
userId =>
{
userId.Map(x => x.Id, "senderId");
}
);
Component(
x => x.receiver,
userId =>
{
userId.Map(x => x.Id, "receiverId");
}
);
Map(x => x.subject);
Map(x => x.text);
}
Now, I do not have to map the User-Class, so I don't have a table only with my Userids.
I'm not too familar with using nHibernate, so I'm not shure wether this is the best way to solve the problem, but in my eyes this fits a bit more to my problem then the solution presented by Chev (but I'm very grateful that you have answered me!)
Change the MessageMap as follows.
public class MessageMap : ClassMap<Message>, IMappedEntity
{
public MessageMap()
{
Id(x => x.Id);
References(x => x.Sender);
References(x => x.Receiver);
Map(x => x.subject);
Map(x => x.text);
}
}

fluent nhibernate component one-to-many

I have couple of classes and want to map them correctly to database:
public class A
{
public virtual Guid Id { get; private set; }
public virtual ComponentClass Component { get; set; }
}
public class ComponentClass
{
public virtual IList<B> Elements { get;set; }
}
public class B
{
public virtual Guid Id { get; private set; }
public virtual DateTime Time { get; set; }
}
I map them using fluent mappings like that:
public class AMap : ClassMap<A>
{
public A() {
Id(x => x.Id);
Component(x => x.Component,
c => c.HasMany(x => x.Elements).Inverse().Cascade.All());
}
}
public class BMap : ClassMap<B>
{
public B() {
Id(x => x.Id);
Map(x => x.Time);
}
}
When I save my entity, I have class A mapped to one table and class B to another as expected.
But I have nulls in Component_id column.
Can you tell me what am I missing here?
I believe Components are supposed to be in the same table , as clearly stated in Ayende's blog post, as they serve only to make the data better represented as an object model. Be sure to read through his blog, it's probably one of the best nHibernate resources out there.
Ok, I've resolved my problem - I can use Id of my "parent" class. So the component mapping will become:
public class AMap : ClassMap<A>
{
public A() {
Id(x => x.Id);
Component(x => x.Component,
c => c.HasMany(x => x.Elements).Cascade.All().Column("Id"));
}
}
So obvious as I look at it now ... but It took me an hour.
If you have a one-to-many association direct to a collection of components (ie. without the ComponentClass wrapper as per the question) then you can map it directly:
HasMany(x => x.Elements)
.AsSet()
.Table("ElementTable")
.KeyColumn("KeyColumn")
.Cascade.All()
.Component(x =>
{
x.Map(c => c.Id);
x.Map(c => c.Time);
})
.LazyLoad();