how to force nhibernate to set the foreign key of the child item? - nhibernate

i have a collection in the mapping:
<bag name="Values" cascade="all-delete-orphan" lazy="false" inverse="true">
<key column="[TemplateId]"/>
<one-to-many class="MyNamespace.Value, MyLib"/>
</bag>
the Value object has a foreign key [TemplateId]. both entities has their generator set to "identity".
when i call session.Save() for the parent Template object, the Value objects has their [TemplateId] (the foreign key) set to zero, so an SQL exception appears.
how do i forse nhibernate to set the FK value for the child items to the value of the inserted parent object?

i've managed it myself:
the only thing i was needed to do is to design child object mapping and persistent the following way:
<many-to-one name="Template" class="MyNamespace.Template, MyLib"
column="[TemplateId]" not-null="true" />
so the child object has a reference to the parent instead of the parent's Id

Related

QueryOver Only <one to many> property

I'm working with NHibernate and QueryOver. I have an aggregate root for my aggregate named Parent and two kinds of child entity. I have Child entities that are parts of my aggregate, and QUChild entities that are not parts of my aggregate, and are just used for JOIN clause in QueryOver.
How to distinguish between two child entities in mapping file?
<class name="Parent" table="Parent" schema="dbo">
<bag name="Childs" inverse="true" cascade="all-delete-orphan" />
<key>
</key>
<one-to-many class="Child" />
</bag>
<bag name="QUChilds" /> <!-- which attribute must be set to do nothing? -->
<key>
</key>
<one-to-many class="QUChild" />
</bag>
</class>
Well, just do nothing with it. NHibernate will not eager load, nor lazy-load it if you do not access it from your loaded parent entities.
And default cascade is none, so just leave it as you have mapped it. (But I would add inverse="true" just in case code changes lead to add some children in that collection too, later on.)

Nhibernate Cannot delete the child object

I know it has been asked for many times, i also have found a lot of answers on this website, but i just cannot get out this problem.
Can anyone help me with this piece of code?
Many thanks.
Here is my parent mapping file
<set name="ProductPictureList" table="[ProductPicture]" lazy="true" order-by="DateCreated" inverse="true" cascade="all-delete-orphan" >
<key column="ProductID"/>
<one-to-many class="ProductPicture"/>
</set>
Here is my child mapping file
<class name="ProductPicture" table="[ProductPicture]" lazy="true">
<id name="ProductPictureID">
<generator class="identity" />
</id>
<property name="ProductID" type="Int32"></property>
<property name="PictureName" type="String"></property>
<property name="DateCreated" type="DateTime"></property>
</class>
Here is my c# code
var item = _productRepository.Get(productID);
var productPictrue = item.ProductPictureList
.OfType<ProductPicture>()
.Where(x => x.ProductPictureID == productPictureID);
// reomve the finding item
var ok = item.ProductPictureList.Remove(productPictrue);
_productRepository.SaveOrUpdate(item);
ok is false value and this child object is still in my database.
Not 100% sure, but could be because you have defined ProductID as a property of ProductPicture, I assume this is the PK from the Product class. You don't need to add this again, it will be created by the relationship.
I'm not sure that your use of table="[ProductPicture]" in the set tag is right.
The one-to-many tag already establishes the link between ProductPictureList and ProductPicture.
I think the table attribute is generally for using a separate relationship table when modelling many-to-may relationships.
From nhibernate.info Doc:
table (optional - defaults to property name) the name of the
collection table (not used for one-to-many associations)
And:
A collection table is required for any collection of values and any
collection of references to other entities mapped as a many-to-many
association

How to query a foreign key column with NHibernate, without retrieving the related entity

Say I have two classes: Parent and Child. A Parent has a property Children, which is of course a collection of Child objects.
Child doesn't have a ParentId property. It does have a Parent property.
So, my NHibernate mapping for Child includes:
<many-to-one name="Parent" class="Parent" column="ParentId" cascade="save-update" />
And my Parent mapping includes:
<bag name="children" access="field" inverse="true" cascade="all-delete-orphan">
<key column="ParentId" />
<one-to-many class="Child" />
</bag>
Now here's what I want to do: I want to get all the Child objects with a certain ParentId. I know I can first get the Parent and then return its Children property. But what if I'd want to query the Child table directly?
If it would be a mapped property (for example, Name), I could use NHibernate's criteria, but in this case, ParentId isn't mapped.
I tried using something like:
criteria.Add(Restrictions.Eq("Parent.Id", 1));
But that doesn't work. I resorted to using SQLCriterion (as explained here), but a friend/colleague got me thinking there must be a better way.
Any ideas? Something with projections and Restrictions.EqProperty?
You have to alias the association path. This will return a proxy for Parent assuming that are using lazy loads. You can access the parent's Id property without triggering a load.
return _session.CreateCriteria<Child>()
.CreateAlias("Parent", "parent")
.Add(Restrictions.Eq("parent.Id", parentId))
.List<Child>();
I've done this using query over. Here is an example:
Child foundChild =
session.QueryOver<Child>()
.Where(x => x.Parent.Id == 1234).SingleOrDefault<Child>();
I think it can be done via Criteria like this:
criteria.Add(Restrictions.Eq("Parent", Session.Load<Parent>(1));

Nhibernate foreign key as composite key

Hi I am pretty new to Nhibernate and I am working on a project that is described as follow:
1 "Parents" table containing ParentName and a one to many ChildrenList using IList, with Primary Key set to ParentId
1 "Children" table , with a composite primary key that contains ParentID as one of the key
so I set up a one to many in Parents.hbm.xml like this
<class name="ParentName " table="Parents">
<id name="ParentName " column="ParentName ">
<generator class="assigned"/>
</id>
<property name="ParentAlpha" />
<bag name="ChildrenList" cascade="all">
<key column="ParentName" />
<one-to-many class="Children"/>
</bag>
and the Children.hbm.xml like this
<composite-id>
<key-many-to-one name="ParentName" class="Parent"/>
<key-property name = "ChildrenAlpha" />
<key-property name = "ChildrenAlpha2"/>
</composite-id>
<property name="ChildrenBeta" />
<property name="ChildrenGama" />
And currently im doing test on saving Parent obj with some list of Children in to a MySQL database using the session.SaveOrUpdate method, but it always fails and just said "cannot insert "the Children Obj
Here is my test code:
Parent parent = new Parent(){ParentAlpha= "ABC"};
Children children = new Children(){ChildrenAlpha = "AAA" ,ChildrenAlpha2 ="VBB"};
parent .ChildrenList.add(children); //IList add function
.....session.SaveOrUpdate(parent);
I have tested for one parent to many children, and the children primary key is set to a generated ChildrenId. and this works fine. But somehow I cannot do it using composite key, my guess is that as ParentName in Children is a primary key but will only be filled after ParentName in Parent has been filled, well sth like that.
Another Question is that if the above problem is solved, can i use this to retrieve the whole parent obj with the children list? ( I tried it for simple single PK case but seems not working when children is a composite key thing)
Parent parent= session.CreateCriteria(typeof(Parent))
.Add(Restrictions.Eq("ParentName", ParentName))
.UniqueResult<Parent>();
NHibernateUtil.Initialize(parent.ChildrenList);
Thanks!

Add/delete item to bag collection

I am working with nHibernate, and trying make sense of bag collections. My data structure is relatively straight-forward...
Entry:
<class name="Entry">
<id name="id" column="EntryId">
<generator type="guid.comb"/>
</id>
<property name="Name" column="Name"/>
<bag name="Results" table="Results" cascade="all">
<key column="EntryId" />
<one-to-many class="Result"/>
</bag>
</class>
Result:
<class name="Result">
<id name="id" column="ResultId">
<generator type="guid.comb"/>
</id>
<property name="Score" column="Score" />
<many-to-one name="Entry" class="Entry" cascade="all" />
</class>
What I would like to do, which doesn't seem to be working, is the following:
Entry entry = new Entry();
entry.Name = "Name";
// have tried saving at this point to:
// dbSession.SaveOrUpdate(entry);
Result result = new Result();
result.Score = 100;
entry.Results.Add(result);
dbSession.SaveOrUpdate(entry);
It seems to be creating the entry record in the database, but not the result record. In my database, I have EntryId as a foreign key in the Result table. Similarly, I would like to be able to remove the result object from the collection, and have it persist to the database. I thought the cascade feature took care of this, but not sure what I have done wrong...
EDIT
I now have it adding the result object into the database, but delete does not seem to work:
Entry entry = Entry.Load(id);
entry.Results.Remove(result);
dbSession.SaveOrUpdate(entry);
I have tried adding cascade="all-delete-orphan", but this seems to remove both parent and children. I just want it to delete the one entry object from the database??
In the end, this came down to my hbm file mappings not being correct.
Entry.hbm.xml
<bag name="Results" table="Result" lazy="false" inverse="true" cascade="all-delete-orphan">
<key column="EntryId"/>
<one-to-many class="Result"/>
</bag>
Result.hbm.xml
<many-to-one name="Entry" class="Entry" column="EntryId"/>
I originally had cascade="all-delete-orphan" on the many-to-one mapping, which was not correct. What happened was that all children and the parent record was being deleted.
I can now add and remove with the following:
Result r = new Result();
Entry entry = new Entry();
// AddResult method sets the Entry object of the Result
// result.Entry = this;
entry.AddResult(r);
session.SaveOrUpdate(entry);
To delete:
entry.Results.Remove(result);
session.SaveOrUpdate(entry);
To add to a collection you need to explicitly save the child object when it is added. Ditto when you delete an object from a collection.
So you would do:
entry.Results.Add(result);
session.Save(result);
session.Save(entry);
session.Flush();
The foreign key also has to be nullable. The reason why you have to do this is NHibernate has to save the child first with no association to the parent. Then when the parent is saved the foreign key column on the child gets updated with the parent's Id, creating the relation. This is because NHibernate may not have the needed parent id key value until the second operation (parent is saved) has completed.
I guess you have this part figured out.
Delete works the same way for different reasons - remove the child from the parent collection, then delete the child explicitly, then update the parent:
entry.Results.Remove(result);
session.Delete(result);
session.Update(entry);
session.Flush();
You removed result from the collection and updated the entry. That only tells Nhibernate to delete the relationship between the entry and the result - you never actually deleted the result object itself.
I notice that, in your collection, you have defined the FK column as:
<key column="EntryId" />
But you are not overriding the column in your many-to-one, which means you have two different columns (Entry and EntryId) for the same relationship.
This might be it or not... but it doesn't hurt to check :-)
if you are using Mapping by Code then use both Cascade.All and Cascade.DeleteOrphans options. unlike the xml mapping, there is no single option for "all-delete-orphan" in Mapping by Code.
Bag(x => x.Results, c =>
{
c.Key(k =>
{
k.Column("EntryId");
});
c.Cascade(Cascade.All | Cascade.DeleteOrphans);
}, r => r.OneToMany())