I use hibernate to store records (i.e. objects) in the database. Before saving my objects, I want to check if the database contains this object . (The primary key is simply an incremental key and cannot be used for this.)
I create an HQL statement at runtime to check for the existence of a record with these attributes (i.e. column1-3).
The resulting query should look like this:
from myTable where column1 is null and column2 = :column2 and column3 = :column3'
Since columns can sometimes contain null values , I check the value of the attributes, if it is NULL, then I use isinstead =in this query (for example, column1 is :column1in the statement above).
Because I’m starting to understand that I’m doing a lot of work to achieve something decisive, I’m starting to wonder if I am on the right track. Is there an easier way to check for the existence of objects?
EDIT: I rephrased my question a bit after I realized that it also column1 is :column1 doesn't work if the parameter is :column1set to null. Apparently, the only syntax that seems to work as expected is column1 is null. Thus, it seems that you simply cannot use wildcards when searching for values null. But that does not change the main aspect of my question: should I really check all this at runtime?
source
share