Casting a string in bool using nHibernate Criteria

I have an nHibernate request using Criteria, and I'm trying to overlay a string on bool in the request itself. I did the same with casting a string to int, and this works well (DataField property β€œ1” as a string):

var result = Session .CreateCriteria<Car>() .Add(Restrictions.Eq((Projections.Cast(NHibernateUtil.Int32, Projections.Property("DataField"), 1)) .List<Car>(); tx.Commit(); 

But I am trying to do the same with bool, but I am not getting the expected result:

 var result = Session .CreateCriteria<Car>() .Add(Restrictions.Eq((Projections.Cast(NHibernateUtil.bool, Projections.Property("DataField"), true)) .List<Car>(); tx.Commit(); 

"DataField" is the string "True", but the result is in the empty list, where it should contain 100 elements with the property string "DataField" set to "True". I tried with the string "true" and "1", but the result is still empty.

[EDIT]

As noted below, I can check the string "True" or "False", but I would say that this is a more general question than just for Boolean ones.

Note that the idea is to have some kind of representation of the values ​​of the key data values, where the value can be different types of data. I need a value table to contain all the data, so saving the data as a string seems like the cleanest solution!

I managed to use the method described above to store both int and double as strings, as well as to cast to the request, but I was not able to use the same method for DateDime and Boolean.

And for DateTime, it is imperative to have an actual DateTime object.

How can I do a translation from a string to bool, and a string in DateTime to work in queries?

thanks

+6
casting nhibernate criteria
source share
1 answer

Unfortunately, what you are trying to achieve will be difficult to do, because it goes against what nHibernate and RDBM are trying to do for you. Using untyped data, you will manage most of the benefits that RDBM gives you.

Without a complete outline, I can only guess. How do you know which type is right for this field? I assume that you have a "type" column that indicates whether the value is an integer, boolean, date, etc. If you do, continue with a type identifier column plus a separate column for each data type. This is no more complicated than what you do, because there are already separate queries for each data type, and you get clarity, type checking, and the ability to index.

If you want to caution against the possibility of defining more than one value (i.e. values ​​in multiple type columns), you can create a table constraint that checks that each row defines a value of at most one data type. (You can also verify that the column for the specified type is not null if this is appropriate for your case.)

Altenratory, you can have nHibernate to manage different types and let it do all the hard work for you. nHibernate can map classes hiearchies to tables, so you can create objects like this:

 public class AbstractProperty { // concrete name - persisted public String Name { get; set; etc.. } // owner property as well? // abstract value provided by subclasses. This property is not persisted. // used simply to provide polymorphic access to the value. public abstract Object Value { get; set; } } public class DateProperty : AbstractProperty { // concrete date property public Date date { get; set; etc.. } // value delegates to date property public Object value { get; set; } } 

Using this scheme, you can get values ​​with a specific data type, where, for example, the query uses the explication of the DateProperty object, returns DateProperty instances. You can also write queries that can return multiple types, where the static type is AbstractProperty . You can then use the visitor pattern or 'is' checks for AbstractProperty to determine the type and apply a specific type to retrieve the value.

Guru nHibernate can help you fix the cast, but in the end, I recommend using real data types in your database. This will save you a headache later.

+5
source share

All Articles