Hibernate: How to set NULL query-parameter value with HQL?

前端 未结 10 749
感情败类
感情败类 2020-12-01 07:23

How can I set a Hibernate Parameter to \"null\"? Example:

Query query = getSession().createQuery(\"from CountryDTO c where c.status = :status  and c.type =:t         


        
10条回答
  •  南方客
    南方客 (楼主)
    2020-12-01 07:51

    1. I believe hibernate first translates your HQL query to SQL and only after that it tries to bind your parameters. Which means that it won't be able to rewrite query from param = ? to param is null.

    2. Try using Criteria api:

      Criteria c = session.createCriteria(CountryDTO.class);
      c.add(Restrictions.eq("type", type));
      c.add(status == null ? Restrictions.isNull("status") : Restrictions.eq("status", status));
      List result = c.list();
      

提交回复
热议问题