使用可选参数在HQL中动态搜索查询?

时间:2016-05-11 04:54:19

标签: java hibernate hql dynamicquery

我正在开发一个我需要搜索功能的应用程序,我想编写根据参数动态创建的HQL查询。 目前我有4个参数,搜索所有参数是必需的,或根据用户想要搜索的方式需要1或2或3个参数。

public List<Plot> fetchSearchedPlots(int plotType, String plotSize, String min, String max)
    {
        Session session = sessionFactory.getCurrentSession();
        List<Plot> searchedLists = new ArrayList<Plot>();
        String query = "FROM Plot where type = ? and size = ? and price >= ? and price <= ?";
        searchedLists = (List<Plot>)session.createQuery( query )
                .setInteger( 0, plotType )
                .setString( 1, plotSize )
                .setString( 2, min )
                .setString( 3, max ).list();
        return searchedLists;
    }

这是我对所有4个参数的一般查询,现在我必须编写一个搜索查询,其中我使用多个可选参数,如何使用可选参数进行此查询?请将我的查询转换为动态可选参数查询? 感谢

3 个答案:

答案 0 :(得分:3)

动态查询的另一个变体是使用Criteria API:

Criteria crit = session.createCriteria(Plot.class);
if (status != null) {
      crit.add(Restrictions.eq("status", status));
}
// other where clauses

对于基于输入的动态条件创建问题的变体:

Criteria criteria = session.createCriteria(Plot.class);;
    if(type != null) {
        criteria.add(Restrictions.eq("type", type));
    }
    if(size != null) {
        criteria.add(Restrictions.eq("size", size));
    }
    if(min != null && max != null) {
        criteria.add(Restrictions.between("price", min, max));
    }
    List<Case> searchedLists  = criteria.list();
    return searchedLists;

答案 1 :(得分:3)

我自己像这样转换了查询

Session session = sessionFactory.getCurrentSession();
        List<Plot> searchedLists = new ArrayList<Plot>();
        Map<String, Object> params = new HashMap<String,Object>();
        String hqlQuery = "from Plot where societyBlock.societyBlockId = :societyBlock";
        params.put( "societyBlock", societyId );
        if(plotType != null)
        {
            hqlQuery += " and type.typeId = :type";
            params.put( "type", plotType );
        }
        if(!plotSize.isEmpty() && plotSize != null && !plotSize.equals( "" ))
        {
            hqlQuery += " and size = :size";
            params.put( "size", plotSize );
        }
        if(min != null)
        {
            hqlQuery += " and price >= :pricemin";
            params.put( "pricemin", min );
        }
        if(max != null)
        {
            hqlQuery += " and price <= :pricemax";
            params.put( "pricemax", max );
        }
        Query query = session.createQuery( hqlQuery );

        for (String str : query.getNamedParameters())
        {
            query.setParameter( str, params.get( str ) );
        }
        searchedLists = (List<Plot>) query.list();
        System.out.println( searchedLists.size() );
        return searchedLists;

答案 2 :(得分:0)

您可以执行以下操作:

    Map<String, Object> parameters= new HashMap<String,Object>();
    parameters.put("status", status);

    StringBuilder hql = "SELECT employee FROM Employee as employee where 1 = 1";
    if (status != null) {
      hql.append(" and employee.status in :status");
    }


    Query query = session.createQuery(hql.toString());


    for (String p : query.getNamedParameters()) {
      query.setParameter(p, parameters.get(p));
    }