NHibernate not inserting parent into the db - nhibernate

When I save a new Report, NHibernate inserts the Report, ignores the Publication and tries to insert the UserPublication. However SQL then complains about violation of FK constraint.
Its like NHibernate doesn't think the Publication is new even though the row doesn't exist in the db.
Think of the entity relationship as:
A Report can have many Publications (Publications belong to a Report)
A Publication can have many UserPublications (UserPublications belong to a Publication)
Any ideas what I've done wrong?
Thanks in advance.
Here's the mappings:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
<class name="Model.Report, Model" table="Report" lazy="true">
<id name="Id" access="property" column="ReportID">
<generator class="assigned"></generator>
</id>
<property name="DeleteUnread" access="property" />
<property name="Description" access="property" />
<property name="Name" access="property" />
<bag name="Publications" access="property" lazy="true" cascade="all-delete-orphan">
<key column="ReportID"/>
<one-to-many class="Model.Publication, Model"/>
</bag>
</class>
</hibernate-mapping>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
<class name="Model.Publication, Model" table="Publication" lazy="true">
<id name="Id" access="property" column="PublicationID">
<generator class="assigned"></generator>
</id>
<property name="CreatedOn" access="property" />
<property name="FileExtension" access="property" />
<property name="IsDownloaded" access="property" />
<property name="ToBeDownloaded" access="property" />
<property name="Name" access="property"/>
<bag name="UserPublications" access="property" lazy="true" cascade="all-delete-orphan">
<key column="PublicationID"></key>
<one-to-many class="Model.UserPublication, Model" />
</bag>
<many-to-one name="Report" class="Model.Report, Model" lazy="false" column="ReportID" not-null="true" cascade="none">
</many-to-one>
</class>
</hibernate-mapping>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
<class name="Model.UserPublication, Model" table="UserPublication" lazy="true">
<id name="Id" access="property" column="UserPublicationID">
<generator class="native"></generator>
</id>
<property name="IsFlaggedForDeletion" access="property" column="IsFlaggedForDeletion" />
<property name="HasBeenRead" access="property" column="HasBeenRead" />
<property name="DateReceived" access="property" column="DateReceived" />
<property name="MustRead" access="property" column="MustRead" />
<property name="ShowToolbar" access="property" column="ShowToolbar" />
<property name="MaxAge" access="property" column="MaxAge" />
<property name="FeedId" access="property" column="FeedId" />
<property name="CanEdit" access="property" column="CanEdit" />
<many-to-one name="User" access="property" column="ClientUserID" class="Model.ClientUser, Model" not-null="true" cascade="none">
</many-to-one>
<many-to-one name="Publication" access="property" class="Model.Publication, Model" column="PublicationID" not-null="true" cascade="none">
</many-to-one>
</class>

I think the problem is that id of publications is an assigned id therefore NHibernate can't recognize when it should insert a publication.
When you flush a session it first insert all inserted objects , then it updates all updated objects and then deletes all deleted objects.
So I think that's going to happen here:
You save a Report that has publications that have userpublications.Since publication id is assigned NHibernate assumes it has to be updated and ignore it but UserPublication id is native and NHibernates knows when it should be inserted and tries to insert it thus a FK violation happens.
To solve this problem you can add a version property to publication so NHibernate can insert it based on its version value.

The UserPublications bag in the Publication class has a wrong key element. It should be:
<key column="PublicationID"/>

This works. I set the unsaved-value attribute to "any".
I don't think there will be any repurcussions.
<id name="Id" access="property" column="PublicationID" unsaved-value="any">
<generator class="assigned"></generator>
</id>

Related

Remove from one side of the many to many in Nhibernate

I have these 2 objects in NHibernate forming a many to many relationship:
User:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Providers" namespace="Providers.Objects">
<class name="User" table="Users">
<id name="UserId" type="int">
<generator class="native" />
</id>
<many-to-one name="Application" column="ApplicationId" cascade="none" />
<property name="UserName" type="string" />
<property name="LoweredUserName" type="string" />
<property name="MobileAlias" type="string" />
<property name="IsAnonymous" type="bool" />
<property name="LastActivityDate" type="DateTime" />
<bag name="Roles" table="UsersInRoles" lazy="true" cascade="none" >
<key column="UserId"></key>
<many-to-many class="Role" column="RoleId"></many-to-many>
</bag>
</class>
</hibernate-mapping>
And Role:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Providers" namespace="Providers.Objects">
<class name="Role" table="Roles">
<id name="RoleId" type="int">
<generator class="native" />
</id>
<many-to-one name="Application" column="ApplicationId" class="Application" cascade="none" />
<property name="RoleName" type="string" />
<property name="LoweredRoleName" type="string" />
<property name="Description" type="string" />
<bag name="Users" table="UsersInRoles" lazy="true" inverse="true" cascade="none" >
<key column="RoleId"></key>
<many-to-many class="User" column="UserId"></many-to-many>
</bag>
</class>
</hibernate-mapping>
Let's say the role backupoperator has some users in it. If I try to remove one of the users from the role instance, like:
var backupoperator = GetRoleByName(session, app.ApplicationId, "backupoperator");
backupoperator.Users.RemoveAt(0);
session.Update(backupoperator);
transaction.Commit();
It doesn't work :( The association remains unchanged in the database. When I try the opposite (remove a role from a user object and updating the user object), it works.
Is it because of the inverse attribute in the NHibernate mapping?
How to accomplish what I am trying to do? (remove a user from a role, updating the role and having that persisted)?
Thanks
When you write inverse="true" you are telling NHibernate that the other side maintains the relationship.
Therefore, you have to remove the Role from the User's Roles collection if you want your change persisted.

Nhibernate - why is it trying to insert existing parent row when inserting child

I don't understand why NHibernate is trying to insert the parent object - when the row already exists in the db - when I'm inserting the child row.
Parent mapping:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
<class name="ReportDistribution.Client.ReportMgr.Model.ClientReport, ReportDistribution.Client.ReportMgr.Model"
table="ClientReport"
lazy="false"
dynamic-update="true">
<id name="Id" access="property" column="ReportID">
<generator class="assigned"></generator>
</id>
<property name="MaxAge" access="property" />
<property name="DeleteUnread" access="property" />
<property name="Description" access="property" />
<property name="Name" access="property" />
<bag name="ClientPublications" cascade="all" lazy="false">
<key column="ReportID" />
<one-to-many class="ReportDistribution.Client.ReportMgr.Model.ClientPublication, ReportDistribution.Client.ReportMgr.Model" />
</bag>
</class>
</hibernate-mapping>
Child mapping:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
<class name="ReportDistribution.Client.ReportMgr.Model.ClientPublication, ReportDistribution.Client.ReportMgr.Model"
table="ClientPublication"
lazy="false"
dynamic-update="true">
<id name="Id" access="property" column="PublicationID">
<generator class="assigned"></generator>
</id>
<property name="CreatedOn" access="property" type="DateTime"></property>
<property name="IsMarkedForDeletion" access="property"></property>
<property name="IsDeleted" access="property"></property>
<property name="HasBeenRead" access="property"></property>
<property name="ReceivedOn" access="property" type="DateTime"></property>
<property name="FileExtension" access="property"></property>
<property name="IsDownloaded" access="property"></property>
<property name="MustRead" access="property"></property>
<many-to-one
name="Report"
class="ReportDistribution.Client.ReportMgr.Model.ClientReport, ReportDistribution.Client.ReportMgr.Model"
lazy="false"
column="ReportID">
</many-to-one>
</class>
</hibernate-mapping>
The Parent class (Report) has property which is a collection of child classes.
The Child class (Publication) has property which is the parent object.
Thanks in advance....
It sounds to me like the parent object is no longer connected to the session when you are saving the child. HNibernate tracks the state of entities that are connected to a session, but if the entity becomes detached it loses the ability to track state.
Think of it like this - if an entity has not come through the exact instance of ISession you are currently using, then it doesn't know it exists. Hence, it treats everything it's never seen as if it were "new."
One option could be to use the ISession.Load(entity); to reload your parent prior to saving.

nhibernate newbe question ... easy one here

I'm new to nhibernate so this should be easy. I have a mapping file as below although I deleted some fields that aren't relevant to this question. The streamfields class contains a bag of fieldmappings. I want the join to be on field_no column but the sql that is sent is on the id field (str_fld_id") as seen below.
I see what the below sql is doing but it's not what I wanted. It's trying to query the field_mappings table based on the values found in the id column str_fld_id in the StreamFields class when I thought it was clear I wanted the field_no to be used on both ends. I say I thought it was clear because the mapping for the field_mapping class has the below attribute and they both have the same named field
Below is in my FieldMappings mapping file.
<many-to-one name="FieldNo" cascade="none" column="`Field_No`" not-null="true">
Sql sent
NHibernate: SELECT fkfieldmap0_.[field_no] as field5_1_, fkfieldmap0_.[Mapping_Id] as Mapping1_1_, fkfieldmap0_.[Mapping_Id] as Mapping1_3_0_, fkfieldmap0_.[Std_fld_Id] as Std2_3_0_, fkfieldmap0_.[Field_Position] as Field3_3_0_, fkfieldmap0_.[Field_No] as Field4_3_0_ FROM [Field_Mappings] fkfieldmap0_ WHERE fkfieldmap0_.[field_no]=#p0; #p0 = '20'
StreamFields mapping
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="DataTransfer.StreamFields,DataTransfer" table="`stream_fields`" lazy="true">
<id name="StrFldId" column="`str_fld_id`" type="int">
<generator class="native" />
</id>
<property type="int" not-null="true" name="FieldNo" column="`field_no`" />
<many-to-one name="StreamId" cascade="none" column="`stream_Id`" />
<bag name="FkFieldMappingsStreamFields" inverse="true" lazy="false" cascade="all">
<key column="`field_no`" />
<one-to-many class="DataTransfer.FieldMappings,DataTransfer"/>
</bag>
</class>
[Edited - with old comments]
Okay, i think i finally got you right and i might admit the problems i had understanding what you want took me a while and result of the lack of information you provided. In the future please provide the mapping of both tables clarify on the point wheather it is a mapping or a query issue. Thx.
IMO you have misunderstood the idea of a parent/child-relation.
The bag you mentioned like to have within the StreamFields class shouldn't be a bag but a direct association. Like this:
<class name="DataTransfer.StreamFields,DataTransfer" table="stream_fields" >
<id name="StrFldId" column="str_fld_id" type="int">
<generator class="native" />
</id>
<property type="int" not-null="true" name="FieldNo" column="field_no" />
<many-to-one name="FieldMapping" class="FiueldMapping" column="Field_No" />
</class>
This of course will only work if you have a property of type FiledMapping in your class.
You want to map FieldMapping to the column Field_No within StreamFields class. There can only be one value within this column, so a bag makes no sense at all. If you want to have a bag of course you can keep it the way it already worked but be aware that the 'key-column' within the bag refers to the child table - in an other way it makes no sense cause a ForeignKey has to map to a PrimaryKey on its parent table. This ensures it is unique and set.
I really don't want to rant but would strongly encourage you to review the hibernate reference about collection mapping to get a deeper clue however.
Hopely this will solve your problem.
Below are the mappings for the classes.
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="DataTransfer.StreamFields,DataTransfer" table="`stream_fields`" lazy="true">
<id name="StrFldId" column="`str_fld_id`" type="int">
<generator class="native" />
</id>
<property type="string" length="50" name="FieldName" column="`field_name`" />
<property type="int" name="InputFieldPosition" column="`input_field_position`" />
<property type="int" name="Start" column="`start`" />
<property type="int" name="Width" column="`width`" />
<property type="string" length="50" name="Datatype" column="`datatype`" />
<property type="int" not-null="true" name="FieldNo" column="`field_no`" />
<property type="int" name="FieldOrder" column="`field_order`" />
<property type="int" name="StdId" column="`Std_Id`" />
<many-to-one name="StreamId" cascade="none" column="`stream_Id`" />
<bag name="FkFieldMappingsStreamFields" inverse="true" lazy="false" cascade="all">
<key column="`field_no`" />
<one-to-many class="DataTransfer.FieldMappings,DataTransfer"/>
</bag>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="DataTransfer.FieldMappings,DataTransfer" table="`Field_Mappings`" lazy="false">
<id name="MappingId" column="`Mapping_Id`" type="int">
<generator class="native" />
</id>
<property type="int" name="StdFldId" column="`Std_fld_Id`" />
<property type="int" name="FieldPosition" column="`Field_Position`" />
<many-to-one name="FieldNo" cascade="none" column="`Field_No`" not-null="true" property-ref="FieldNo" />
</class>
</hibernate-mapping>
To make things easy on myself, there is one record in stream_fields and the field_no value is 1 and 20 is the value in StrFldId.
SELECT fkfieldmap0_.[field_no] as field5_1_, fkfieldmap0_.[Mapping_Id] as Mapping1_1_, fkfieldmap0_.[Mapping_Id] as Mapping1_3_0_, fkfieldmap0_.[Std_fld_Id] as Std2_3_0_, fkfieldmap0_.[Field_Position] as Field3_3_0_, fkfieldmap0_.[Field_No] as Field4_3_0_ FROM [Field_Mappings] fkfieldmap0_ WHERE fkfieldmap0_.[field_no]=#p0; #p0 = '20' –

Nhibernate bag is not saved

I'm trying to save an object that contains a bag with a many to many relationship.
The problem is that the object is saved correctly but the bag isn't.
Here is my mapping, can anyone tell me what I'm doing wrong.
Thanks
<class name="XManager.Business.Afspraak, XManager.Business" table="Afspraak" lazy="false" mutable="false">
<id name="Id" type="int">
<generator class="identity" />
</id>
<property name="StartDatum" column="StartDatum"/>
<property name="EindDatum" column="EindDatum"/>
<property name="Herhalend" column="Herhalend"/>
<property name="HerhaalInterval" column="HerhaalInterval"/>
<property name="Opmerking" column="Opmerking"/>
<property name="Status" column="Status"/>
<property name="ToevoegDatumTijd" column="ToevoegDatumTijd"/>
<property name="WijzigDatumTijd" column="WijzigDatumTijd"/>
<many-to-one name="Klant" column="KlantId" class="Klant"/>
<bag name="Behandelingen" table="AfspraakBehandelingen" lazy="false" cascade="all-delete-orphan">
<key column="AfspraakId"/>
<many-to-many class="Behandeling" column="BehandelingId"/>
</bag>
<class name="XManager.Business.Behandeling, XManager.Business" table="Behandeling" lazy="false" mutable="false">
<id name="Id" type="int">
<generator class="identity" />
</id>
<property name="Naam" column="Naam"/>
<property name="Omschrijving" column="Omschrijving"/>
<property name="Duur" column="Duur"/>
<property name="Prijs" column="Prijs"/>
<property name="ToevoegDatumTijd" column="ToevoegDatumTijd"/>
<property name="WijzigDatumTijd" column="WijzigDatumTijd"/>
<many-to-one name="Categorie" column="CategorieId" class="Categorie"/>
Try inverse=true in the bag definition

NHibernate Many-to-many

I have a legacy database and I am trying to create a NHibernate DAL.
I have a problem with a mapping on Many-To-Many table.
The Database tables:
studio_Subscribers
studio_Groups (contains a IList of Subscribers)
studio_Subscribers_Groups - Many-To-Many table with primary keys
The problem is when I create a SubscriberGroup instance and fill it with Subscribers they gets saved to the studio_Subscribers table but not to the Many-To-Many table.
I cant figure out whats wrong?
studio_Subscribers table mapping:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Meridix.Studio.Common"
namespace="Meridix.Studio.Common">
<class name="SubscriberItem" table="studio_Subscribers">
<id name="StorageId" column="Id" unsaved-value="0" access="nosetter.camelcase">
<generator class="identity" />
</id>
<property name="Id" column="DomainId" not-null="true" />
<property name="Subscriber" column="Subscriber" not-null="true" length="50" />
<property name="Description" column="Description" not-null="false" length="100" />
<property name="Type" column="Type" not-null="true" length="40"
type="Meridix.Studio.Data.Repositories.EnumStringTypes.SubscriberTypeEst, Meridix.Studio.Data.Repositories" />
</class>
</hibernate-mapping>
studio_Groups table mapping:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Meridix.Studio.Common"
namespace="Meridix.Studio.Common">
<class name="SubscriberGroup" table="studio_Groups">
<id name="StorageId" column="Id" unsaved-value="0" access="nosetter.camelcase">
<generator class="identity" />
</id>
<property name="Id" column="DomainId" not-null="true" />
<property name="Name" column="Name" not-null="true" length="200" />
<property name="Description" column="Description" not-null="false" length="300" />
<bag name="Subscribers" table="studio_Groups_Subscribers" access="nosetter.camelcase">
<key column="GroupId"></key>
<many-to-many column="SubscriberId" class="SubscriberItem" />
</bag>
</class>
</hibernate-mapping>
Shouldn't you have an appropriate bag on your subscriber aswell with a many-to-many relation to the group?
<bag name="Groups" table="studio_Groups_Subscribers" access="nosetter.camelcase">
<key column="SubscriberId"></key>
<many-to-many column="GroupId" class="GroupItem" />
</bag>
and maybe have a cascade="save-update" on your SubscriberItems collection so that saving the Group will update your children with the appropriate relation from "the other side".
I believe it is to do with the cascade options, check out Ayende's post on it and I imagine with a bit of Googling you can find the correct syntax to go into your hbm file. I use fluent-NHibernate so I can't help you with the XML file.
http://ayende.com/Blog/archive/2006/12/02/NHibernateCascadesTheDifferentBetweenAllAlldeleteorphansAndSaveupdate.aspx