如何在Hibernate中使用setParameterList()方法? [英] How to use the setParameterList() method in Hibernate?

查看:111
本文介绍了如何在Hibernate中使用setParameterList()方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据 ids 从Oracle数据库中提取选定的行作为数组,例如 SELECT ... FROM table_name WHERE id IN()查询。

在我这样做的尝试中,我试图使用 org.hibernate.setParameterList(String name,Object [ ]值) 方法在我的DAO如下。

  @Service 
@Transactional(readOnly = true,propagation = Propagation.REQUIRES_NEW)
public final class ProductImageDAO implements ProductImageService {

@SuppressWarnings(unchecked)
public List< Object [] > getFileName(String [] list){
return sessionFactory
.getCurrentSession()
.createQuery(SELECT prodImageId,prodImage FROM ProductImage WHERE prodImageId =:list)
.setParameterList list,list).list();




$ b

类型参数 String给定方法中的[] 由相应的Spring控制器类提供。



它会引发以下异常。


org.hibernate.hql.ast.QuerySyntaxException:意外标记:,在
附近第1列第78列[select prodImageId,prodImage from
model.ProductImage其中prodImageId =:id0_,:id1_,:id2_,:id3_,
:id4_,:id5 _]


使用Hibernate根据 ids 列表检索选定行的方法是什么?

解决方法

 字符串queryString =从cgix.trust.domain.PtbnAccount中选择acc作为acc where acc.accountId IN(:accountdIds); 
Query query = session.createQuery(queryString);
query.setParameterList(accountIds,accountFilter);

假设 accountFilter List 对象。请记住,不应该传递空列表,因为这会导致以下SQL(这将不起作用): ... WHERE xyz IN()... (注意空的in-clause)。


I have a requirement to fetch selected rows from Oracle database based on ids supplied as an array, something like the SELECT ... FROM table_name WHERE id IN() query.

In my attempts to do so, I'm trying to use the org.hibernate.setParameterList(String name, Object[] values) method in my DAO as follows.

@Service
@Transactional(readOnly = true, propagation=Propagation.REQUIRES_NEW)
public final class ProductImageDAO implements ProductImageService {

    @SuppressWarnings("unchecked")
    public List<Object[]> getFileName(String[] list) {
        return sessionFactory
                .getCurrentSession()
                .createQuery("SELECT prodImageId, prodImage FROM ProductImage WHERE prodImageId=:list")
                .setParameterList("list", list).list();
    }
}

The parameter of type String[] in the given method is supplied from the respective Spring controller class.

It causes the following exception to be thrown.

org.hibernate.hql.ast.QuerySyntaxException: unexpected token: , near line 1, column 78 [select prodImageId, prodImage from model.ProductImage where prodImageId=:id0_, :id1_, :id2_, :id3_, :id4_, :id5_]

What is the way to retrieve the selected rows based on list of ids using Hibernate?

解决方案

String queryString = "select acc from cgix.trust.domain.PtbnAccount as acc where acc.accountId IN (:accountdIds)";
Query query = session.createQuery (queryString);
query.setParameterList("accountIds", accountFilter);

Assuming accountFilter is a List object. Keep in mind that you should not pass empty lists since that would result in the following SQL (which won't work): ... WHERE xyz IN () ... (note the empty in-clause).

这篇关于如何在Hibernate中使用setParameterList()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆