简体   繁体   English

hibernate,检查对象是否存在以及null值

[英]hibernate, check if object exists and null values

I am using hibernate to save records (ie objects) to a database. 我正在使用hibernate将记录(即对象)保存到数据库中。 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 (ie column1-3 ). 在运行创建一个HQL语句来检查具有这些属性的记录的存在性(即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 (eg the column1 is :column1 in the above statement). 因为有时列可以包含空值 ,我检查属性的值,如果它是一个空值,那么我使用is代替=在此查询(例如, column1 is :column1在上面的语句)。

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 . 编辑: 我稍微改写我的问题后,我意识到, column1 is :column1时不起作用:column1参数设置为null Apparently the only syntax that seems to work as expected is column1 is null . 显然,唯一似乎按预期工作的语法是column1 is null So, it seems like you just cannot use wildcards when searching for null values. 因此,在搜索null值时似乎无法使用通配符。 But that does not change the main aspect of my question: should I really be checking all this stuff at runtime ? 但这并没有改变我的问题的主要方面:我应该在运行时检查所有这些东西吗?

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 (ie map.put("column1", Integer.valueOf(1)) ). 键指的是属性(即map.put("column1", Integer.valueOf(1)) )。

There is a Restritions.eqOrIsNull method that simplifies the conversion to a Criterion object. 有一个Restritions.eqOrIsNull方法,简化了对Criterion对象的转换。 The following method converts an entire Map to a List<Criterion> . 以下方法将整个Map转换为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. 稍后,我使用List<Criterion>来构建Criteria对象。

 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 (eg Criteria#addAll(List<Criterion>) would have been nice). 我的总体印象仍然是这里缺少几种便利方法(例如Criteria#addAll(List<Criterion>)本来不错)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM