We have 2 tables/classes lets call them NewOne and OldOne. OldOne has Id type of int and NewOne has property OldValues tape of OldOne and string SourceType which is mapped (database is Oracle):
<many-to-one name="OldValues" column="SOURCE_ID" />
<property name="SourceType " column="Source_Type " type="AnsiString" />
We want to get data from NewOne and OldOne when SourceType='Old', but the problem is that ID column of OldOne table is number and Source_Id column is varchar which stores other types of ids like names etc. We cannot change type of ID neither Source_ID columns. Such join can be easily done in sql using to_char() function:
left outer join OldOne old_
on this_.SOURCE_ID = to_char(old_.ID)
and (this_.Source_Type = 'Old')
But I cannot find equivalent of to_char in QueryOver. Below is the version with conditional join but without to_char():
OldOne OldOneAlias = null;
NewOne NewOneAlias = null;
return this.ActiveSession.QueryOver<NewOne>(() => NewOneAlias)
.JoinAlias(r => r.OldValues,
() => OldOneAlias,
JoinType.LeftOuterJoin,
Restrictions.Eq(
Projections.Property(() => NewOneAlias.SourceType), "Old"))
Is there any way to create query with to_char using QueryOver?
Update:
I have found a little bit ugly solution here: "Safe" TO_NUMBER() and after changing my mapping everything works:
<many-to-one name="OldValues" formula="COALESCE(TO_NUMBER(REGEXP_SUBSTR(SOURCE_ID, '^\d+')), 0)" />
The main problem was that previous query ignores "and (this_.Source_Type = 'Old')" and tries to make join with OldOne even when Source_Type != Old and SOURCE_ID isn't number.Not sure if it is only Oracle's issue.