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

前端 未结 10 747
感情败类
感情败类 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:50

    this seems to work as wel ->

    @Override
    public List findAllForThisSpecificThing(String thing) {
        final Query query = entityManager.createQuery(
                "from " + getDomain().getSimpleName() + " t  where t.thing = " + ((thing == null) ? " null" : " :thing"));
        if (thing != null) {
            query.setParameter("thing", thing);
        }
        return query.getResultList();
    }
    

    Btw, I'm pretty new at this, so if for any reason this isn't a good idea, let me know. Thanks.

提交回复
热议问题