I am using hibernate to save records (i.e. objects) to a database. Before saving my objects, I want to verify if the database already contains this object. (The primary key is just an incremental key and cannot be used for this.)
I am creating a HQL statement at runtime to check the existance of a record with these attributes (i.e. column1-3
).
The resulting query should look like:
from myTable where column1 is null and column2 = :column2 and column3 = :column3'
Because sometimes the columns can contain null values, I check the value of the attributes, if it is a NULL value, then I use a is
instead of a =
in this query (e.g. the column1 is :column1
in the above statement).
Because I start to realize that I am doing a lot of work to achieve something reletively crucial, I am starting to wonder if I'm on the right track. Is there an easier way to check the existance of objects ?
EDIT: I slightly rephrased my question after I realized that also column1 is :column1
does not work when :column1
parameter is set to null
. Apparently the only syntax that seems to work as expected is column1 is null
. So, it seems like you just cannot use wildcards when searching for null
values. But that does not change the main aspect of my question: should I really be checking all this stuff at runtime ?