首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >参数列表中参数的Hibernate HQL属性

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

Stack Overflow用户
提问于 2015-04-11 21:51:31
回答 1查看 1.1K关注 0票数 0

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

代码语言:javascript
复制
<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();
    }

}
EN

回答 1

Stack Overflow用户

发布于 2015-04-11 21:58:23

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

查询:

代码语言:javascript
复制
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)

代码:

代码语言:javascript
复制
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();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29578636

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档