Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate, check if object exists and null values

Tags:

java

hibernate

h2

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 ?

like image 846
bvdb Avatar asked Nov 10 '22 08:11

bvdb


1 Answers

This is the best way that I found so far.

I prefer to put my filters in a map. The key refers to the property (i.e. map.put("column1", Integer.valueOf(1))).

There is a Restritions.eqOrIsNull method that simplifies the conversion to a Criterion object. The following method converts an entire Map to a List<Criterion>.

public List<Criterion> mapToCriterion(Map<String, Object> params)
{
  if (params == null) return null;

  // convert the parameter map to a list of criterion
  List<Criterion> criterionList = new ArrayList<>(params.size());
  for (Map.Entry<String, Object> entry : params.entrySet())
    criterionList.add(Restrictions.eqOrIsNull(entry.getKey(), entry.getValue()));
  return criterionList;
}

Later on, I use the List<Criterion> to build a Criteria object.

 Criteria criteria = session.createCriteria(clazz);
 if (criterionList != null)
 {
   for(Criterion criterion : criterionList) 
     criteria.add(criterion);
 }

 // get the full list
 @SuppressWarnings("unchecked")
 List<T> objectList = criteria.list();

My general impression is still that there are missing several convenience methods here (e.g. Criteria#addAll(List<Criterion>) would have been nice).

like image 197
bvdb Avatar answered Nov 14 '22 23:11

bvdb