Tips and Tricks in a world of Mix

Posts tagged ‘Fluent Nhibernate’

Fluent Nhibernate – fail-safe cleanup (collections) warning

 

My mapping was in CustomerMutzar :

  HasMany<CustomerMutzarKishur>(x => x.Col_CustomerMutzarKishur)
                 .Access
                 .Property()
                 .AsSet()
                 .KeyColumn("CMK_CMU_RECID")
                 .Cascade
                 .All().LazyLoad().Fetch.Join().Inverse();

 

Class Diagram of my entity model was :

The mistake I’ve got in NHibernate Profiler :

it does the select query :

SELECTFROM   CUSTOMER_MUTZAR customermu0_
       left outer join CUSTOMER_MUTZAR_KISHUR col_custom1_
         on customermu0_.CMU_RECID = col_custom1_.CMK_CMU_RECID
       left outer join CUSTOMER_MUTZAR_PRODUCT customermu2_
         on col_custom1_.CMK_CMP_RECID = customermu2_.CMP_RECID
WHERE  customermu0_.CMU_RECID = 450 /* :p0 */

 

 
and then I get few problematic statements in profiler :

WARN:
fail-safe cleanup (collections) : NHibernate.Engine.Loading.CollectionLoadContext<rs=System.Data.OracleClient.OracleDataReader>

WARN:
On CollectionLoadContext#cleanup, localLoadingCollectionKeys contained [1] entries

and a system crash from log  :

StackTrace: NHibernate.PropertyAccessException: Invalid Cast (check your mapping for property type mismatches); setter of Ness.DoarKamuti.Infrastructure.Entities.CustomerMutzar —> System.InvalidCastException: Unable to cast object of type ‘NHibernate.Collection.Generic.PersistentGenericSet`1[Ness.DoarKamuti.Infrastructure.Entities.CustomerMutzarKishur]’ to type ‘System.Collections.Generic.IList`1[Ness.DoarKamuti.Infrastructure.Entities.CustomerMutzarKishur]’.
   at (Object , Object[] , SetterCallback )

The Solution :

I went again to the mapping of the entity and adjusted the relationships :

 

 HasMany<CustomerMutzarKishur>(x => x.Col_CustomerMutzarKishur).AsBag()
                .KeyColumn("CMK_CMU_RECID")
                .LazyLoad().Fetch.Join().Inverse();

This has resolved the issue for me.

 

Fluent Nhibernate configuring OptimistickLock.Dirty() with Transaction.IsolationLevel.ReadCommited when the row locked in Oracle 10g

The problem is :
I have a working system with FluentNhibernate.
I have a legacy database and it’s hybrid system which suppose to give an answer to a new and old systems as one. So that means I couldn’t use the Version – which means at this point to change the schema and add column to each table.

I’ve added OptimistickLock.Dirty() with Transaction.IsolationLevel.ReadCommited() to deal with the problem of locking .
The trouble is that when the row locked in Oracle 10g and I am trying to SaveOrUpdate the entity it just stops the application.

Then , when I save or rollback in Oracle the Application gets the process back and throws the exception NHibernate.StaleObjectStateException that is being handled in my exception handler which is fine.

The question is how to make the application to recognize on the spot that there is a lock on the current row I am trying to access and throw the adequate exception .

Fluent NHibernate Sequence Strategy with Oracle

The environment :
Working with Oracle 10g.
At the mapping files of the FNH on the ID column define the sequence to work with.
This is a relevant sequence used by the relevant trigger used for the current table.
The db is a legacy db – using triggers to increment the records on the insert.
The problem :
When I save the record I’m getting back the record number. Well I’m getting the expected number +1 , every time . The record is being inserted and the record number jumps two times instead of one.
The solution:
In the trigger you must enter check to see if the new value is null , and only then increment the trigger . If it’s not null it means it was incremented by FNH already and there is no need to activate the increment in the trigger.

CREATE OR REPLACE TRIGGER DOAR_DEV.CUSTOMER_TRG1
BEFORE INSERT
ON DOAR_DEV.CUSTOMER REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
WHEN (
NEW.CST_RECID IS NULL
)

DECLARE
tmpVar NUMBER;
BEGIN
tmpVar := 0;

SELECT Customer_SEQ.NEXTVAL INTO tmpVar FROM dual;
:NEW.Cst_RecID := tmpVar;

EXCEPTION
WHEN OTHERS THEN
— Consider logging the error and then re-raise
RAISE;
END ;
/

Fluent NHibernate HasMany PropertyRef solution

For a long time tried to find the solution for HasMany relationship not through ID property of the entity .
Starting from Fluent NHibernate build 164 there is a way , no patches included –
HasMany(x => x.Collection_TChild).AsBag()
.KeyColumn(“CHILDPROPERTY_TOJOINBY”)
.PropertyRef(“Parent_Property”);

p.s.
Crazy thing , after including the FNH.dll of 164 version , we were compelled to add
column property to the Mapping . So , it means that we went through all our mappings
and added

Map(x => x.Property).Column(“PropertyNameAsItAppearsInOracleDataBase”);

otherwise the NHibernate didn’t recognized the mappings…

Tag Cloud

%d bloggers like this: