【问题标题】:Hibernate HQL attribute of a parameter in parameter list参数列表中参数的 Hibernate HQL 属性
【发布时间】:2015-06-17 04:30:51
【问题描述】:

我有一个如下所示的 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();
    }

}

【问题讨论】:

    标签: hibernate hql named-query


    【解决方案1】:

    你不能那样做。使您的查询具有 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();
    }
    

    【讨论】:

    • 这三个参数之间的关系很重要,比如哪个值在哪个id中。我们如何定义关系?
    • 我已经修改了我的答案,但我不确定这是否是您真正想要的。如果不是这样,那么您应该首先考虑 SQL 查询应该是什么样子,然后将其转换为 HQL,如果可能的话。
    猜你喜欢
    • 2014-01-29
    • 1970-01-01
    • 2011-01-09
    • 2017-12-30
    • 2011-09-16
    • 1970-01-01
    • 2013-03-10
    • 1970-01-01
    • 2012-09-26
    相关资源
    最近更新 更多