简体   繁体   中英

Spring Hibernate generate dynamic query

I am using hibernate spring where I need to generate query on a condition.

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. 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.

        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.

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);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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