简体   繁体   English

Spring Hibernate生成动态查询

[英]Spring Hibernate generate dynamic query

I am using hibernate spring where I need to generate query on a condition. 我正在使用休眠弹簧,需要在此条件下生成查询。

DAO.java DAO.java

public ReturnData updateUserDetails(Users users, String mailID)
{
    if(!users.getImageURL().equals(""))
    {
        Query query = this.sessionFactory.getCurrentSession().createQuery("UPDATE users SET emailID=:email_ID, name=:name, imageURL=:imageURL WHERE emailID=:emailID")
        //setString....

    }
    else
    {
         Query query = this.sessionFactory.getCurrentSession().createQuery("UPDATE users SET emailID=:email_ID, name=:name WHERE emailID=:emailID")
        //setString....

    }
}

In the above code, I check if image also has been uploaded or not. 在上面的代码中,我检查图像是否也已上传。 On the basis of this condition, I have to dynamically generate query. 在这种情况下,我必须动态生成查询。 I have to rewrite the whole code for query+execution 2 times. 我必须重写整个代码以进行查询和执行2次。 Is it the good way, or is there any better way to do this? 这是好方法,还是有更好的方法呢?

You can dynamically append the query conditions to the query string if they are not null. 如果查询条件不为空,则可以将查询条件动态附加到查询字符串中。 After getting the final list of conditions, you can create Hibernate query. 在获得最终条件列表之后,您可以创建Hibernate查询。

        StringBuilder sqlQuery = new StringBuilder();
        Map<String,Object> parameters = new HashMap<String,Object>();
        boolean isFirstSearchCriterion = true;
        sqlQuery.append("UPDATE users");


        if(email_ID!= null && !email_ID.trim().equals("")) {
            if(isFirstSearchCriterion) {
                sqlQuery.append(" set emailID= :email_ID");
            } else {
                sqlQuery.append(" and emailID= :email_ID");
            }
            parameters.put("email_ID",email_ID);
            isFirstSearchCriterion = false;
        }

        if(name!= null && !name.trim().equals("")) {
            if(isFirstSearchCriterion) {
                sqlQuery.append(" set name= :name");
            } else {
                sqlQuery.append(" and name= :name");
            }
            parameters.put("name",name);
            isFirstSearchCriterion = false;
        }

        if(imageURL!= null && !imageURL.trim().equals("")) {
            if(isFirstSearchCriterion) {
                sqlQuery.append(" set imageURL= :imageURL");
            } else {
                sqlQuery.append(" and imageURL= :imageURL");
            }
            parameters.put("imageURL",imageURL);
            isFirstSearchCriterion = false;
        }


          Query query = this.sessionFactory.getCurrentSession().createQuery(sqlQuery);


        Set<String> parameterSet = parameters.keySet();
        for (Iterator<String> it = parameterSet.iterator(); it.hasNext();) {
            String parameter = it.next();
            query.setParameter(parameter, parameters.get(parameter));
        }

You can simply do without checking empty String, if user has image url it will add in column or else empty url will be pass on. 您可以简单地执行而不检查空字符串,如果用户具有图像URL,它将添加到列中,否则将传递空URL。

public ReturnData updateUserDetails(Users users, String mailID)
{
    Query query = this.sessionFactory.getCurrentSession().createQuery("UPDATE users SET emailID=:email_ID, name=:name, imageURL=:imageURL WHERE emailID=:emailID")
       query.setParameter("imageURL",users.getImageURL(), Hibernate.STRING);

}

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

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