Fluent NHibernate insert parent id problem with HasMany relationship - nhibernate

I can't understand why NHibernate is inserting a child entity without the foreign key of the parent.
The only way I found to solve this is with a Bidirectional relationship, is there another way?
Here are the classes:
public class Parent
{
public virtual int ParentId {get; private set;}
public virtual IList<Child> Notes {get; private set;}
}
public class Child
{
public virtual ChildId {get; private set;}
public virtual Name {get; private set;}
}
Here is my Fluent NHibernate mapping
public class ParentClassMap : ClassMap<Parent>
{
public ParentClassMap(){
Id(x => x.ParentId);
HasMany(x => x.Notes).Cascade.AllDeleteOrphan();
}
}
public class ChildClassMap : ClassMap<Child>
{
public ChildClassMap() {
Id(x => x.ChildId);
Map(x => x.Name);
}
}
When I add a child to the parent's child collection and save the parent, the parent and the child are inserted into the database, but the child is inserte withoutthe foreign key to the parent (it has a null value)
This is the insert that is generated:
INSERT INTO Child(ChildId, Name)
But it should be:
INSERT INTO Child(ChildId, Name, ParentId)
I want to add that i don't want to resolve this with a bidirectional relationship, i do not want the child to have a reference to the parent. Thanks!!

Add Not.KeyNullable() to your HasMany mapping.
Note: This feature requires NHibernate 3.2.

Your parent class mapping should have an inverse for its child
public class ParentClassMap : ClassMap<Parent>
{
public ParentClassMap(){
Id(x => x.ParentId);
HasMany(x => x.Notes).Cascade.AllDeleteOrphan().Inverse();
}
}
Thanks
Neelesh

Related

NHibernate fluent mapping to an entity property

I have a table called "Customers" and in this table there are store and office address. In the code there is a Customer class with two properties that are of type Address (one for StoreAddress and OfficeAddress).
public class Customer
{
public virtual int Id { get; set;}
public virtual string Name {get;set;}
public virtual string Email {get; set;}
public virtual Address StoreAddress {get; set;}
public virtual Address OfficeAddress {get; set;}
}
public class Address
{
public string Address1 {get; set;}
public string Address2 {get; set;}
public string State {get; set;}
public string City {get; set;}
public string Zip {get; set;}
}
I can map items that are not of an entity type Address but not sure how to map to another entity property within the customer entity?..
Table("Customers");
Schema("dbo);
Id(x => x.ID).Column("CustomerId");
Map(x => x.Name);
Map(x => x.Email);
How would I be able to map to my StoreAddress and OfficeAddress from the table Customers table?
You can use component mapping:
Component(x => StoreAddress).ColumnPrefix("StoreAddress");
Component(x => OfficeAddress).ColumnPrefix("OfficeAddress");
Then create a component map for Address type:
public class AddressMap : ComponentMap<Address>
{
public AddressMap()
{
//map properterties
}
}
In case, I understand your missing point, we can use:
References / many-to-one
References is for creating many-to-one relationships between two entities, and is applied on the "many side." You're referencing a single other entity, so you use the References method. #HasMany / one-to-many is the "other side" of the References relationship, and gets applied on the "one side."
Table("Customers");
Schema("dbo);
Id(x => x.ID).Column("CustomerId");
Map(x => x.Name);
Map(x => x.Email);
// this is the fluent way for many-to-one
References(x => x.StoreAddress);
References(x => x.OfficeAddress);
The complete overview of the References() syntax could be found here: Mapping-by-Code - ManyToOne (by Adam Bar) - which is about mapping-by-code, but provides comparison with fluent syntax (the second half of the post)

Fluent nhibernate m-to-m mapping with external table

I have two tables in Oracle
Entity
----------
**EntityId** NUMBER(9), **EntityName** VARCHAR2
EntityLinks
--------------
**EntityLinkId** NUMBER(9),**ParentEntityId** NUMBER(9), **ChildEntityId** NUMBER(9)
Table EntityLinks will store ManyToMany relationship between various entities. ParentEntityId and ChildEntityId are having foreign key relationship with Entity.
I have below a below class for Entity as well
public class Entity
{
public virtual int EntityId {get; set}
public virtual IList<Entity> ParentEntities {get; set}
public virtual IList<Entity> ChildEntities{get; set}
}
public class EntityLinks
{
public virtual int EntityLinkId {get; set}
public virtual Entity ParentEntityId {get; set}
public virtual Entity ChildEntityId {get; set}
}
Here is the mapping for both the classes:
public class EntityMap : ClassMap<Entity>
{
public EntityMap()
{
Table("Entity")
Id(x=>x.EntityId).GeneratedBy.Increment();
*---- How to map for ParentEntities and ChildEntites?----*
}
}
public class EntityLinksMap : ClassMap<Entity>
{
public EntityMap()
{
Table("Entity")
Id(x=>x.EntityId).GeneratedBy.Increment();
References(x=>x.ParentEntityId).Column("ParentEntityId");
References(x=>x.ChildEntityId).Column("ChildEntityId");
}
}
My question is how should I do mapping in entity class for ParentEntities and ChildEntites so that I get the list of both parent and child for a particular entity?
I would say that it is really good, that you are using man-in-the-middle table as a standard mapped entity.
I am talking about the fact, that we can use mapping without that pairing table being represented as mapped entity. The syntax would be HasManyToMany
But because you've chosen to have pairing table as an entity you have to change the Business model:
public class Entity
{
public virtual int EntityId {get; set}
//public virtual IList<Entity> ParentEntities {get; set}
//public virtual IList<Entity> ChildEntities{get; set}
public virtual IList<EntityLinks> ParentEntityLinks {get; set}
public virtual IList<EntityLinks> ChildEntityLinks {get; set}
}
Mapping then would be:
public EntityMap()
{
Table("Entity")
Id(x=>x.EntityId).GeneratedBy.Increment();
HasMany(x => x.ParentEntityLinks)...
HasMany(x => x.ChildEntityLinks)...
}
More about HasMany mapping:
Mapping-by-Code - Set and Bag by Adam Bar
Scroll down to Fluent NHibernate's equivalent:
HasMany(x => x.Users)
.AsSet<CustomComparer>() // or .AsSet(), .AsBag()
.Fetch.Join()
.BatchSize(100)
.LazyLoad() // or .ExtraLazyLoad()
.Table("tableName")
.Schema("schemaName")
.Cascade.AllDeleteOrphan() // or .None(), .SaveUpdate(), .All(), DeleteOrphan()
.Inverse()
...
// and many more settings
...
If in fact, you really want to keep the many-to-many, and properties like
public virtual IList<Entity> ParentEntities {get; set}
public virtual IList<Entity> ChildEntities{get; set}
The mapping will be
HasManyToMany(x => x.ParentEntities)...
HasManyToMany(x => x.ChildEntities)...
...which I would not suggest to use: How to create NHibernate HasManyToMany relation or Nhibernate: How to represent Many-To-Many relationships with One-to-Many relationships?, Here are some more details how to use HasManyToMany if ...
I figured out the mappings to be use. I was just confused in the problem at hand since this is self-referencing.
public class EntityMap : ClassMap<Entity>
{
public EntityMap()
{
Table("Entity")
Id(x=>x.EntityId).GeneratedBy.Increment();
//solution mapping
HasManyToMany(x => x.ChildEntities)
.Table("EntityLinks")
.ParentKeyColumn("ParentEntityId")
.ChildKeyColumn("ChildEntityId");
HasManyToMany(x => x.ParentEntities)
.Table("EntityLinks")
.ParentKeyColumn("ChildEntityId")
.ChildKeyColumn("ParentEntityId");
}
}
public class EntityLinksMap : ClassMap<EntityLinks>
{
public EntityMap()
{
Table("EntityLinks")
Id(x=>x.EntityId).GeneratedBy.Increment();
References(x=>x.ParentEntityId).Column("ParentEntityId");
References(x=>x.ChildEntityId).Column("ChildEntityId");
}
}

NHibernate Save Is Trying to Clear Child KeyColumn Id On Update

I am trying to create a parent object that has multiple children in a 1 to many relationship. I am not referencing the Parent object on the child object, instead I am mapping its keycolumn as a field.
When I try to save this object for the first time, it works as expected without any issues (cascading all the Id's and children). When I try to get the object from the database, update some properties on it and re-save it again, it fails. The actual error message I am getting is "Could not delete collection".
The error above is due to the fact that it is trying to set the "ParentId" field on the child objects to NULL (which violates the FK constraint I have in the db). If I remove this constraint from the DB, the end result is what I want; however, I do not want it to perform this update (setting parent id to null) at all and I'm not sure why it is. From what I can tell in the SQL code it is generating and sending to the DB, everything else appears to be correct and it would all work if it wasnt for that last update statement.
Obviously, I must have something wrong with my mapping but I cannot figure out what. I tried adding Not.KeyUpdate() but that simply made it not generate a key at all. Does anyone have any ideas what I am doing wrong??
Thanks in advance, I really appreciate it!!!
Please see below for my mapping:
public class Parent
{
public Parent()
{
Children = new List<Child>();
}
public virtual Guid Id { get; set; }
public virtual IList<Child> Children { get; set; }
}
public class Child
{
public virtual Guid Id { get; set; }
public virtual Guid ParentId { get; set; }
}
public class ParentMap : ClassMap<Parent>
{
public ParentMap()
{
Table("Parent");
Id(x => x.Id);
HasMany(x => x.Children).KeyColumn("ParentId").Cascade.SaveUpdate().Not.LazyLoad();
}
}
public class ChildMap : ClassMap<Child>
{
public ChildMap()
{
Table("Child");
Id(x => x.Id);
Map(x => x.ParentId);
}
}
This is caused by the fact, that the Collection of children is not marked as inverse="true".
What you can do is: I. to remove the constraint from DB. NHiberante simply must (without the inverse setting) do 2 steps. Firstly update record to break relation, secondly (due to cascades) alse delete the item
II. Change the mapping, and entities. Like this:
A Child must have reference to the Parent:
public class Child
{
public virtual Guid Id { get; set; }
public virtual Guid ParentId { get; set; }
public virtual Parent Parent { get; set; }
}
Mapping then will be like this:
public class ParentMap : ClassMap<Parent>
{
public ParentMap()
{
Table("Parent");
Id(x => x.Id);
HasMany(x => x.Children)
.KeyColumn("ParentId")
.Cascade.SaveUpdate()
.Not.LazyLoad()
.Inverse() // here we do have the inverse setting
;
}
}
public class ChildMap : ClassMap<Child>
{
public ChildMap()
{
Table("Child");
Id(x => x.Id);
Map(x => x.ParentId).Not.Insert().Not.Update(); // this is readonly now
References(x => x.Parent).Column("ParentId"); // just a Parent is writable
}
}
Now, you have to all the time properly set the relation in the C# code. I.e. if child is added to Parents collection, it should also get set the Parent reference
parent.Children.Add(child);
child.Parent = parent;
NHibernate will now issue only one statement, to delete child from its table

Map list of items

I have a table called openTickets. I have another table called openTicketFollowers that relates to it using a foreign key. OpenTickets does not know about openTicketFollowers but I want openTickets to have a property that is a list of its followers. Is there anyway to do this with fluent nhibernate?
Check this Fluent mapping document. The OpenTicket class will contain IList of Followers:
public class OpenTicket
{
...
public virtual IList<OpenTicketFollower> Followers { get; set; }
}
public class OpenTicketFollowers
{
public virtual OpenTicket OpenTicket { get; set; }
}
And this is fluent mapping of the OpenTicketFollowercollection:
HasMany(x => x.Followers)
.KeyColumn("OpenTicketId");
and the OpenTicketFollower class mapping referencing the OpenTicket
References(x => x.OpenTicket)
.Column("OpenTicketId")

Fluent NHibernate: How to create one-to-many bidirectional mapping?

Basic question: How to I create a bidirectional one-to-many map in Fluent NHibernate?
Details:
I have a parent object with many children. In my case, it is meaningless for the child to not have a parent, so in the database, I would like the foreign key to the parent to have NOT NULL constraint. I am auto-generating my database from the Fluent NHibernate mapping.
I have a parent with many child objects like so:
public class Summary
{
public int id {get; protected set;}
public IList<Detail> Details {get; protected set;}
}
public class Detail
{
public int id {get; protected set;}
public string ItemName {get; set;}
/* public Summary Owner {get; protected set;} */ //I think this might be needed for bidirectional mapping?
}
Here is the mapping I started with:
public class SummaryMap : ClassMap<Summary>
{
public SummaryMap()
{
Id(x => x.ID);
HasMany<Detail>(x => x.Details);
}
}
public class DetailMap : ClassMap<Detail>
{
public DetailMap()
{
Id(x => x.ID);
Map(x => x.ItemName).CanNotBeNull();
}
}
In the Detail table, the Summary_id should be Not Null, because in my
case it is meaningless to have a Detail object not attached to the
summary object. However, just using the HasMany() map leaves the Summary_id foreign key nullable.
I found in the NHibernate docs (http://www.hibernate.org/hib_docs/nhibernate/html/collections.html) that "If the parent is required, use a bidirectional one-to-many association".
So how do I create the bidirectional one-to-many map in Fluent NHibernate?
To get a bidirectional association with a not-null foreign key column in the Details table you can add the suggested Owner property, a References(...).CanNotBeNull() mapping in the DetailsMap class, and make the Summary end inverse.
To avoid having two different foreign key columns for the two association directions, you can either specify the column names manually or name the properties in a way that gives the same column name for both directions. In this case you I suggest renaming the Details.Owner property to Details.Summary.
I made the Summary id generated by increment to avoid problems when inserting into the table since Summary currenty has no columns besides id.
Domain:
public class Detail
{
public int id { get; protected set; }
public string ItemName { get; set; }
// Renamed to use same column name as specified in the mapping of Summary.Details
public Summary Summary {get; set;}
}
public class Summary
{
public Summary()
{
Details = new List<Detail>();
}
public int id { get; protected set; }
public IList<Detail> Details { get; protected set; }
}
Mapping:
public class DetailMap : ClassMap<Detail>
{
public DetailMap()
{
Id(x => x.id)
.GeneratedBy.Native();
Map(x => x.ItemName)
.CanNotBeNull();
References<Summary>(x => x.Summary)
// If you don't want to rename the property in Summary,
// you can do this instead:
// .TheColumnNameIs("Summary_id")
.CanNotBeNull();
}
}
public class SummaryMap : ClassMap<Summary>
{
public SummaryMap()
{
Id(x => x.id)
.GeneratedBy.Increment();
HasMany<Detail>(x => x.Details)
.IsInverse()
.AsBag(); // Use bag instead of list to avoid index updating issues
}
}