如何存储我从Postgres存储函数中获取的数据

时间:2015-01-16 19:07:50

标签: java hsqldb

我目前正在调用存储函数,如下所示:

SQLQuery query = session.createSQLQuery(my_function())

此函数返回一个表:

 RETURNS TABLE(
        userid            BIGINT,
        user_height    DOUBLE PRECISION
    )

如何获取两列并将它们存储到2个数组列表中?

我试过:

List<Object[]> data= query.list();
        List<Long> result= newArrayList();

        for (Object[] each: data)
        {
            Long userId= (Long) point[0];
            result.add(userId);

        }

我得到了exception: org.hibernate.exception.SQLGrammarException: user lacks privilege or object not found: MY_FUNCTION()

1 个答案:

答案 0 :(得分:0)

CALL您的存储过程并将结果映射到一类对象,如下例所示:

class UserHeight
{
  public long userid;
  public double user_height;
}

Query query = session.createSQLQuery("CALL my_function()")
    .addEntity(UserHeight.class);

List result = query.list();
for(int i=0; i<result.size(); i++){
    UserHeight height = (UserHeight)result.get(i);
    System.out.println(height.height);
}