Mapping a non existing column in Nhibernate - c#

I am working with an Oracle legacy database and I am trying to map a relationship with two tables. Unfortunately the company who designed the database used composite keys.
I've got this table (ORDERS) which has a primary key composed on 3 fields: ordernumber (number), version and company.
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="BpSalesOrders.Domain"
namespace="BpSalesOrders.Domain">
<class name="Order" table="OCSAORH" mutable="false" where="OCHAMND = 0">
<cache usage="read-only"/>
<composite-id>
<key-property name="Number" column="OCHORDN" type="String" length="10"></key-property>
<key-property name="Ver" column="OCHAMND" type="Int32"></key-property>
<key-property name="Company" column="OCHCOSC" type="String" length="5"></key-property>
</composite-id>
<property name="Reference" column="OCHOCNO" type="String" length="25"></property>
<property name="Date" column="OCHOCDT" type="Int32"></property>
<property name="Status" column="OCHSTA1" type="Char"></property>
<property name="LineOfCreditStatus" column="OCHSTA4" type="Char"></property>
<property name="WareHouse" column="OCHRESP" type="String"></property>
<set name="OrderLines" access="field.pascalcase-underscore" inverse="true" lazy="true" mutable="false">
<key>
<column name="OCLORDN" not-null="true"/>
<column name="OCLAMND" not-null="true" default="0"/>
<column name="OCLCOSC" not-null="true"/>
</key>
<one-to-many class="OrderLine" not-found ="ignore"/>
</set>
<set name="OrderReleases" access="field.pascalcase-underscore" inverse="true" lazy="true" mutable="false">
<key>
<column name="ORLORDINE" not-null="true" />
<column name="ORLSOCIETA" not-null="true"/>
</key>
<one-to-many class="OrderRelease" not-found ="ignore"/>
</set>
</class>
</hibernate-mapping>
There's a relation with OrderLines which works fine cause that table has the same exact key structure.
I am having problem with the OrderReleases relation. That table has got a primary key composed on a OrderNumber, Company and date. There's no version here:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="BpSalesOrders.Domain"
namespace="BpSalesOrders.Domain">
<class name="OrderRelease" table="OCBPORDRIL" mutable="true">
<composite-id>
<key-property name="Company" column="ORLSOCIETA" type ="String" length="5"></key-property>
<key-property name="OrderNumber" column="ORLORDINE" type ="String" length="10"></key-property>
<key-property name="RequestDate" column="ORLDATARICHIESTA" type ="Int32"></key-property>
</composite-id>
<property name="Status" column="ORLSTATUS" type="Char" length="1"></property>
<property name="StatusMessage" column="ORLSEGNALAZIONE" type="String" length="90"></property>
<property name="ProcessingDate" column="ORLDATAELABORA" type ="Int32"></property>
</class>
</hibernate-mapping>
Considering that the relationship is one-to-many I would like to join the table Orders and OrderRelease on OrderNumber and Company. If I try to do that (as in the example) nHibernate throws an exception telling me that the key it's trying to map is composed with 3 fields.
Is there a way to create an non-existing column which wouldn't be persisted so that I can setup my mapping and relations?
Any help will be apreciated.

Ended up creating a view with a fake column.

Related

ORA 01407 - Error - Cannot update to NULL

My problem is that i have two entities (Document and Attach) that has an relationship one-to-one. In my app, i can save first the Document and lately if i want, i can attach an archive to it, that will be on the table Attach.
The error occurs when i have an object Document already inserted in database and then i try to add an attach on it.
Below is the nhibernate mapping:
Document.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Sigre.Business"
namespace="Sigre.Business.BusinessEntity">
<class name="Document" lazy="false" table="grsds.documento_fcdr">
<id name="Code" type="int" unsaved-value="0" column="docf_sq_documento_fcdr">
<generator class="sequence">
<param name="sequence">grsds.sq_docf_sq_documento_fcdr</param>
</generator>
</id>
<property name="Nome" type="AnsiString" length="100" not-null="true" column="docf_nm_documento_fcdr" />
<many-to-one name="Manifest" class="TransportManifest" column="mtra_sq_manifesto_transporte" not-null="false" cascade="none" />
<many-to-one name="User" class="User" column="user_id" not-null="false" cascade="none" />
<set name="Attach" inverse="false" lazy="false" cascade="save-update">
<key column="docf_sq_documento_fcdr" />
<one-to-many class="Attach" />
</set>
</class>
</hibernate-mapping>
Attach.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Sigre.Business"
namespace="Sigre.Business.BusinessEntity">
<class name="Attach" lazy="false" table="grsds.espec_documento_fcdr">
<id name="Code" type="int" unsaved-value="0" column="docf_sq_documento_fcdr">
<generator class="foreign">
<param name="property">Document</param>
</generator>
</id>
<property name="Archive" column="esdf_mm_documento_fcdr" type="BinaryBlob" not-null="true" />
<one-to-one constrained="true" name="Document" access="property" />
</class>
</hibernate-mapping>
If i am inserting both Document and Attach together, it works. But when i have a Document created and try to insert an Attach, the following error occurs:
ORA-01407: cannot update ("GRSDS"."ESPEC_DOCUMENTO_FCDR"."DOCF_SQ_DOCUMENTO_FCDR") to NULL
What i tried:
1 - Save the Document having the attach to insert:
bmDocument.Save(document);
Ok i solved this question.
When i bring the object Document from database, the attribute Attach when has no Attach, come as an empty list: {}
When i tried to add an Attach to this Document, i was overwriting the reference, like this:
Document.Attach = new List() { new Attach() };
The solution was use the method Add of the list that was brought from the database:
Document.Attach.Add( new Attach() );

Nhibernate One To Many With Criteria

I've got a class which contains a list of associated objects.
I've map it as follows:
<class name="MyDto" table="`MyView`" mutable="false" lazy="false">
<id name="Id" column="ID" type="int">
<generator class="assigned" />
</id>
<set name="Days" cascade="all-delete-orphan" lazy="false" >
<key column="Id" />
<one-to-many class="MyAssociatedObj"/>
</set>
</class>
<class name="MyAssociatedObj" table="V_SC_NEED" mutable="false" lazy="false" >
<id name="Id" column="`ID2`" type="int">
<generator class="assigned" />
</id>
<property type="DateTime" not-null="true" name="DayDate" column="`Date`" />
<component name="Audit">
<property type="string" not-null="true" length="255" name="Username" column="`USERNAME`" />
<property type="decimal" not-null="true" name="PreviousValue" column="`PrevVal`" />
</component>
</class>
What I wanted to do is to get a list of MyDto Object, filtering by my associated object, MyAssociatedObject.
Which means, for example, if i do:
UnitOfWork.Session.CreateCriteria<MyDto>().List<MyDto>()
I get a list of 42 lines.
Now, I want to filter them by my associated object, by a restriction of between.
the problem is that when i do so:
UnitOfWork.Session.CreateCriteria<MyDto>().CreateAlias("Days", "days").Add(Restrictions.Between("days.DayDate", query.FromDate, query.ToDate));
I'm getting, somehow 684 rows ! (some cartesial product or something).
Any advise?
Many thanks !
You are getting more rows in the second query because you are JOINing with MyAssociatedObj, which seems to be in a many-to-one relationship with your MyDTO class.
If you want to filter out duplicates of MyDTO, you can use the DistinctRootResultTransformer:
UnitOfWork.Session.CreateCriteria<MyDto>().CreateAlias("Days", "days")
.Add(Restrictions.Between("days.DayDate", query.FromDate, query.ToDate))
.SetResultTransformer(
new NHibernate.Transform.DistinctRootEntityResultTransformer());
Hope it helps.

NHibernate Tree structure update root problem

I have a tree structure mapped to the data base in a table like this:
UID numeric(18, 0)
NodeUID varchar(50)
Text nvarchar(50)
TreeLevel int
ParentUID varchar(50)
OrderInLevel int
IsLeaf bit
It is an old table and I cannot change it so bear with me...
The NodeUID is a GUID.
the ParentUID column is mapped to the NodeId of a different row.
A root node has a value of "0" in its ParentUID column.
I am trying to map the Tree with NHibernate like this:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="BasicFW.Core.Domain.NavigationTree.UsersNavigationTreeNode, BasicFW.Core" table="bDoxTreeNodes" lazy="false" schema="bDox.dbo">
<id name="NodeUId">
<column name="NodeUID"/>
<generator class="assigned"/>
</id>
<property name="Text">
<column name="Text"/>
</property>
<property name="TreeLevel">
<column name="TreeLevel"/>
</property>
<property name="IsLeaf">
<column name="IsLeaf"/>
</property>
<many-to-one name="Parent" class="BasicFW.Core.Domain.NavigationTree.UsersNavigationTreeNode, BasicFW.Core" column="ParentUID" not-found="ignore"/>
<bag name="Children" lazy="false" order-by="OrderInLevel ASC" cascade="all-delete-orphan" inverse="true">
<key column="ParentUID"/>
<one-to-many class="BasicFW.Core.Domain.NavigationTree.UsersNavigationTreeNode, BasicFW.Core" />
</bag>
</class>
</hibernate-mapping>
The thing is that when I try to update a node ( and a root node in specifies ) the Parent property is null and so NHibernate try`s to Update the ParentUID column to null, and so it failles since to column will not accept null.
thanks
Can you create a trigger for that table? If yes, you could perhaps create a trigger that fires before update/insert. Something like:
create trigger TreeNode_before before insert, update
on TreeNode
referencing NEW as _new
for each row
begin
if _new.ParentUID is null then
set _new.ParentUID = '0';
end if;
end;
Edit: An alternative would be using an interceptor. I have not used that myself but according to the following question it should work in your case, too.
Edit: fixed the link.
NHibernate write value = NULL when ID < 0
I have found the correct mapping that worked, here is the mapping for my tree.
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="BasicFW.Core.Domain.NavigationTree.UsersNavigationTreeNode, BasicFW.Core" table="TreeNodes" lazy="false" >
<id name="NodeID">
<column name="NodeUID"/>
<generator class="assigned"/>
</id>
<property name="Text">
<column name="Text"/>
</property>
<property name="TreeLevel">
<column name="TreeLevel"/>
</property>
<property name="ParentID" >
<column name="ParentUID" />
</property>
<property name="IsLeaf">
<column name="IsLeaf"/>
</property>
<many-to-one name="Parent" class="BasicFW.Core.Domain.NavigationTree.UsersNavigationTreeNode, BasicFW.Core" column="ParentUID" not-found="ignore" update="false" insert="false"/>
<bag name="Children" lazy="false" order-by="OrderInLevel ASC" inverse="true" cascade="all-delete-orphan">
<key column="ParentUID"/>
<one-to-many class="BasicFW.Core.Domain.NavigationTree.UsersNavigationTreeNode, BasicFW.Core"/>
</bag>
</class>
</hibernate-mapping>
I have disabled the update and insert in the Parent property.

NHibernate Many-to-Many Mapping not working

I have a Nhibernate mapping file for a simple user/role mapping.
Here are the mapping files:
Users.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Sample.Persistence" namespace="Sample.Persistence.Model">
<class name="User" table="Users">
<id name="UserKey">
<generator class="identity"/>
</id>
<property name="UserName" column="UserName" type="String" />
<property name="Password" column="Password" type="Byte[]" />
<property name="FirstName" column="FirstName" type="String" />
<property name="LastName" column="LastName" type="String" />
<property name="Email" column="Email" type="String" />
<property name="Active" column="Active" type="Boolean" />
<property name="Locked" column="Locked" type="Boolean" />
<property name="LoginFailures" column="LoginFailures" type="int" />
<property name="LockoutDate" column="LockoutDate" type="DateTime" generated="insert" />
<property name="Expired" column="Expired" type="Boolean" generated="insert"/>
<set name="Roles" table="UsersRolesBridge" lazy="false">
<key column="UserKey" />
<many-to-many class="Role"
not-found="exception"
column="RoleKey" />
</set>
</class>
</hibernate-mapping>
Role.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Sample.Persistence" namespace="Sample.Persistence.Model">
<class name="Role" table="Roles">
<id name="RoleKey">
<generator class="identity"/>
</id>
<property name="Name" column="Name" type="String" />
<set name="Users" inverse="true" table="UsersRolesBridge" lazy="false" >
<key column="RoleKey" />
<many-to-many class="User" column="UserKey" />
</set>
</class>
</hibernate-mapping>
I am able to retrieve roles for each user via NHibernate but when I go to save a new object, the roles are not saved in the Bridge table.
The user is created and insert with no issues. I've checked that the Role collection, a field on the user, is being populated with the proper rolekey before the Session.Save() is called.
There is no exception thrown as well.
UPDATE:
After adding a cascade, there was still no actual insert taking place to the M-2-M table.
The logs from Nhibernate showed the following:
12/22/2010 23:18:11.684 [11] INFO NHibernate.Engine.Cascade
Message: processing cascade NHibernate.Engine.CascadingAction+SaveUpdateCascadingAction for: Sample.Persistence.Model.User
Exception:
12/22/2010 23:18:11.686 [11] INFO NHibernate.Engine.Cascade
Message: done processing cascade NHibernate.Engine.CascadingAction+SaveUpdateCascadingAction for: Sample.Persistence.Model.User
Exception:
12/22/2010 23:18:11.789 [11] INFO NHibernate.Engine.Cascade
Message: processing cascade NHibernate.Engine.CascadingAction+SaveUpdateCascadingAction for: Sample.Persistence.Model.User
Exception:
12/22/2010 23:18:11.792 [11] INFO NHibernate.Engine.Cascade
Message: cascade NHibernate.Engine.CascadingAction+SaveUpdateCascadingAction for collection: Sample.Persistence.Model.User.Roles
Exception:
12/22/2010 23:18:11.814 [11] INFO NHibernate.Engine.Cascade
Message: done cascade NHibernate.Engine.CascadingAction+SaveUpdateCascadingAction for collection: Sample.Persistence.Model.User.Roles
Exception:
12/22/2010 23:18:11.814 [11] INFO NHibernate.Engine.Cascade
Message: done processing cascade NHibernate.Engine.CascadingAction+SaveUpdateCascadingAction for: Sample.Persistence.Model.User
You haven't specified a cascade on the relationship. By default, inserts, updates, and deletes are not cascaded. More information here:
http://ayende.com/Blog/archive/2006/12/02/nhibernatecascadesthedifferentbetweenallalldeleteorphansandsaveupdate.aspx
I generally find that many to many issues are caused by not setting the cascade properties properly.
Try setting the cascade = "save-update" attribute on the Roles set in the Users.hbm.xml file
<set name="Roles" table="UsersRolesBridge" lazy="false" cascade="save-update">

Joining NHibernate Classes that share a common column but no foreign key

I have a couple of tables that I want to map to classes. The tables look like this:
Asset
---------
AssetId
AssetName
Product
---------
ProductId
ProductName
AssetId
Disposal
---------
DisposalId
AssetId
DisposalDate
Basically what I want to do is join the Product Table to the Disposal table on AssetId so that my Product has a collection of Disposals joined by asset. I have defined the following mapping but NHibernate (1.2) seems to ignore the key column defined in the bag and chooses to join the Product table to the Disposal table by ProductId (ie Product.ProductId = Disposal.AssetId). I'm not sure if this is a bug or if I'm not defining it properly but if anyone has a way to do this I'd be most greatful.
<class name="Product" table="Product" lazy="false">
<id name="ProductId" column="ProductId" type="int">
<generator class="native" />
</id>
<property name="ProductName" column="ProductName"/>
<bag name="Disposals" fetch="join" >
<key column="AssetId" foreign-key="AssetId/>
<many-to-many class="Disposal"/>
</bag>
</class>
Have you got your Disposals mapped to Products?
Your schema doesn't unique relate a Disposal to a Product. A Disposal can only relate to an Asset, not a Product.
You schema says to me that an Asset has many Products, and an Asset has many Disposals. There's nothing that says a Disposal is for a particular product.
The clean way:
<class name="Product" table="Product" lazy="false">
<id name="ProductId" column="ProductId" type="int">
<generator class="native" />
</id>
<property name="ProductName" column="ProductName"/>
<many-to-one name name="Asset" class="Asset" column="AssetId" />
</class>
<class name="Asset">
<id name="AssetId" >
<generator class="native" />
</id>
<property name="AssetName" />
<bag name="Disposals">
<key column="AssetId" />
<many-to-many class="Disposal" />
</bag>
</class>
foreign-key is used for DDL, I think it is the name of the foreign key constraint generated by schema export.
You can try property-ref, not completely sure if it works:
<class name="Product" table="Product" lazy="false">
<id name="ProductId" column="ProductId" type="int">
<generator class="native" />
</id>
<property name="ProductName" column="ProductName"/>
<property name="AssetId" />
<bag name="Disposals" fetch="join" >
<key column="AssetId" property-ref="AssetId/>
<one-to-many class="Disposal"/>
</bag>
</class>

Categories

Resources