Reputation: 495
I have an entity A with a bool property (lets call it BProp). In a second entity there is a bag mapping of A elements with a where condition on BProp like this:
<bag name="MyBag" cascade="all-delete-orphan" inverse="false" where="BProp = 1">
<key column="Structure_Id" />
<one-to-many entity-name="A" />
</bag>
The problem is tha BProp = 1 works with SqlServer and Oracle but breaks PostgreSQL which needs a condition like
where="BProp = true"
Is there a clever way to create a single hbm.xml mapping for all the three db I have to suppport?
Upvotes: 2
Views: 1238
Reputation: 15413
As the query substition does not seem to be a usable solution in this filter case, you may go for query interceptor (here a quite rough implementation) :
public class BooleanInterceptor : EmptyInterceptor, IInterceptor
{
public string TrueToken { get; set; }
public string FalseToken { get; set; }
NHibernate.SqlCommand.SqlString IInterceptor.OnPrepareStatement(NHibernate.SqlCommand.SqlString sql)
{
return sql.Replace("{TrueToken}", TrueToken).Replace("{FalseToken}", FalseToken);
}
}
Your mapping would then be where="BProp = {TrueToken}"
It would be used like this
var interceptor = new BooleanInterceptor ();
using (var session = sessionFactory.OpenSession(interceptor))
{
interceptor.FalseToken = "0"; // this replacement value should be taken from a config file
interceptor.TrueToken = "1"; // see above
// your code here
session.Close();
}
I bet there are way better solutions, but I hope this will help anyway
-------------------------- previous answer
I use to insert query substitutions in my NHibernate configuration, like this
var cfg = new Configuration();
cfg.Properties.Add("dialect", "NHibernate.Dialect.MsSql2005Dialect");
.
cfg.Properties.Add("query.substitutions", "true 1, false 0");
.
I guess you could have your xml mapping with where="BProp = true"
and just insert the cfg property for query.substitions depending if the dialect is MsSql / Oracle or PosgresSQL
Upvotes: 1