参数列表中参数的Hibernate HQL属性

时间:2015-04-11 13:51:31

标签: hibernate hql named-query

我有一个如下的HQL。当我运行这个HQL时,我得到错误。我想使用对象列表作为参数。我想要使​​用这个对象属性。我已经通过使用Criteria和DetachedCriteria解决了这个问题,但我想知道HQL中有任何解决方案。

<named-query name="Book.findBooksByPropertyValue">
    <query>
        select book
        from Book book
        join book.bundleProperty bundleProperty
        join bundleProperty.property property
        left join bundleProperty.selectedProperty selectedProperty
        where
        property.code in :bundleProperty.property.code and 
        (bundleProperty.propertyValue in :bundleProperty.propertyValue or selectedProperty.id in :bundleProperty.selectedProperty.id)
    </query>
</named-query>


class Book()
{
    //...
    BundleProperty bundleProperty;
}

class BundleProperty()
{
    ...
    boolean propertySelective;
    Property property;
    String propertyValue;
    PropertyChoice selectedProperty;
}

class bookController()
{
    //...
    public List<Book> findBooks()
    {
        List<BundleProperty> bundleProperties = new ArrayList<>();
        for(PropertyBase propertyBase : selectedPropertyBases )
        {
            BundleProperty bundleProperty = new BundleProperty();
            bundleProperty.setProperty(propertyBase.getProperty());

            if(propertyBase.getProperty().isPropertySelective())
            {
                bundleProperty.setSelectedProperty(propertyBase.getSelectedProperty());
            }
            else
            {
                bundleProperty.setPropertyValue(propertyBase.getPropertyValue());
            }
            bundleProperties.add(bundleProperty);
        }

        return bookService.findBooksByPropertyValue(bundleProperties);
    }
}

class bookServiceImp()
{
    //...
    public List<Book> findBooksByPropertyValue(List<BundleProperty> bundleProperties)
    {
        return getSession().getNamedQuery("Book.findBooksByPropertyValue")
                .setParameterList("bundleProperty", bundleProperties)
                .list();
    }

}

1 个答案:

答案 0 :(得分:0)

你做不到。使您的查询有3个不同的参数(:value:code:id),并在执行时,从BundleProperty对象中提取这3个参数,并将每个参数设置为参数。

查询:

select book
    from Book book
    join book.bundleProperty bundleProperty
    join bundleProperty.property property
    left join bundleProperty.selectedProperty selectedProperty
    where
    property.code in :codes and 
    (bundleProperty.propertyValue in :values or selectedProperty.id in :ids)

代码:

public List<Book> findBooksByPropertyValue(List<BundleProperty> bundleProperties) {
    List<String> codes = new ArrayList<>();
    List<String> ids = new ArrayList<>();
    List<String> values = new ArrayList<>();

    for (BundleProperty bp : bundleProperties) {
        codes.add(bp.getProperty().getCode());
        ids.add(bp.getSelectedProperty().getId());
        values.add(bp.getPropertyValue());
    }

    return getSession().getNamedQuery("Book.findBooksByPropertyValue")
            .setParameterList("ids", ids)
            .setParameterList("codes", codes)
            .setParameterList("values", values)
            .list();
}