3
votes

In my application I have a bool property named DisplayIndicator. In the database (DB2) it's correspondence is DISPL_IND column of type smallint. The correspondence is the following: [DisplayINdicator=True, DISPL_IND=1] and [DisplayINdicator=False, DISPL_IND=0] Is it possible to map using nhibernate fluence the bool property to smallint?

2
Doesn't this just work? In MySQL it's the same thing - a boolean is mapped as a tinyint. I'm just using bool in my code and never had to do anything special. - simendsjo
It doesn't know to convert true to 1. It generates the query with value "True" and in the database it fails. - Raul
Ok, must be a bit different from MySQL then. MySQL has the types TRUE and FALSE that maps to 1 and 0 - simendsjo

2 Answers

2
votes

I figured it out, after Frans's advise. I created a class that represents nhibernate user type used to map boolean type to short type:

public class BooleanAsShortType : IUserType

To the mapping, I added a CustomType property and now it looks like this:

Map(x => x.DisplayIndicator, "DSPL_IND").CustomType< BooleanAsShortType >();

1
votes

You could implement IUserType on a class and convert bool to short and back and apply it to the field where you need it.