Tips and Tricks in a world of Mix

Archive for the ‘Fluent Nhibernate’ Category

Fluent Nhibernate Trigger-Identity problem

In our case we work with Oracle 10g with FluentNhibernate 1.0.0.597 and NHibernate 2.1.2.4000 .
We have a legacy system based on the db used in our new website .
It means any change on the db may influence the old system.
When we mapped through GeneratedBy.Sequence(“My_Sequence”) , the sequence progressed but it progressed twice – once by NHibernate and once by the trigger on the table.
solution :
Advertisements

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: