java - 在 JPA 存储库中调用 (Postgres) 函数

标签 java postgresql jpa

我正在尝试调用我在 Postgres 数据库中定义的函数。

CREATE OR REPLACE FUNCTION bacon()
  RETURNS text AS
$BODY$
SELECT cast('ingeborg' as text)
$BODY$
  LANGUAGE sql STABLE;
ALTER FUNCTION bacon()
  OWNER TO my_user;

在我的 Spring 存储库中,我尝试调用该函数

@Repository
public class ItemRepository {

  @PersistenceContext(name = "com.project.persistenceUnit")
  private EntityManager entityManager;

 public Collection<ItemView> findBy() {
    String statement = "SELECT * FROM schema.bacon()";

     String result = (String) entityManager.createNativeQuery(statement)
        .getSingleResult();
  }

}

当我尝试从存储库中获取数据时出现以下错误

11.04.2014 11:34:00.177 [main] WARN  o.h.e.jdbc.spi.SqlExceptionHelper:144 @ SQL Error: 0, SQLState: 42883
11.04.2014 11:34:00.177 [main] ERROR o.h.e.jdbc.spi.SqlExceptionHelper:146 @ ERROR: Function schema.bacon() does not exist

SQLState 42883 是“undefined_function”(http://www.postgresql.org/docs/9.3/static/errcodes-appendix.html)

该函数在 pgAdmin 中调用时有效。

我做错了什么?如何调用定义的函数?

更新

我连接到错误的数据库,必须遵循以下 post :

public class MyExtendedPostgresSQLDialect extends PostgreSQL9Dialect {

    public MyExtendedPostgresSQLDialect() {
        super();
        registerFunction("bacon", new PostgresBacon());
    }

}
public class PostgresBacon implements SQLFunction {

    @Override
    public boolean hasArguments() {
        return false;
    }

    @Override
    public boolean hasParenthesesIfNoArguments() {
        return false;
    }

    @Override
    public Type getReturnType(Type firstArgumentType, Mapping mapping) throws QueryException {
        return TextType.INSTANCE;
    }

    @Override
    public String render(Type firstArgumentType, List arguments, SessionFactoryImplementor factory) throws QueryException {
        return "bacon()";
    }

}

最佳答案

我认为当您创建函数和调用函数时,您的 search_path 设置不相等。 对于 exp :

digoal=# show search_path;
  search_path   
----------------
 "$user",public
(1 row)
digoal=# \c digoal digoal
You are now connected to database "digoal" as user "digoal".
digoal=> create function f_test1() returns void as $$
digoal$> declare
digoal$> begin
digoal$> end;
digoal$> $$ language plpgsql;
CREATE FUNCTION
digoal=> select f_test1();
 f_test1 
---------

(1 row)

digoal=> \c digoal postgres
You are now connected to database "digoal" as user "postgres".
digoal=# select f_test1();
ERROR:  function f_test1() does not exist
LINE 1: select f_test1();
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
digoal=# \dn
  List of schemas
  Name  |  Owner   
--------+----------
 digoal | postgres
 public | postgres
(2 rows)

digoal=# \df digoal.f_test1
                         List of functions
 Schema |  Name   | Result data type | Argument data types |  Type  
--------+---------+------------------+---------------------+--------
 digoal | f_test1 | void             |                     | normal
(1 row)

digoal=# \df public.f_test1
                       List of functions
 Schema | Name | Result data type | Argument data types | Type 
--------+------+------------------+---------------------+------
(0 rows)

关于java - 在 JPA 存储库中调用 (Postgres) 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23011452/

相关文章:

java - 什么是 NullPointerException,我该如何解决?

java - 为什么这段代码不抛出 ConcurrentModificationException?

java - Spring roo,字段枚举

r - 使用 RPostgresql 打印查询时间

java - Spring - 更改 persist_logins 的架构连接

java - 理解模式和匹配器

sql - 当 select 没有行时获取案例结果字段

python - 在 SQLAlchemy 中查询 View

mysql - Hibernate @JoinColumn insertable=false 不起作用(重复的列名)

java - EclipseLink 查询 - 从学生中选择计数(*)不工作