通过nativeQuery创建一个函数

时间:2013-05-07 20:57:25

标签: java eclipselink nativequery

我正在尝试通过本机查询在数据库中插入一个函数,比如

EntityManagerImpl entityManagerFunctionConta = (EntityManagerImpl) GermantechEntityManager.getEntityManager();
EntityTransaction transactionFunctionConta = entityManagerFunctionConta.getTransaction();
String functionConta = "CREATE OR REPLACE FUNCTION saldo_anterior_conta(dt_inicial date, id_conta bigint, id_empresa bigint) " +
 "RETURNS numeric AS " +
 "$BODY$ " +
 "declare " +
 "saldo_anterior numeric(14,2);" +
 "begin " +
 "select coalesce(sum(valor), 0) into saldo_anterior " +
 "from saldoinicialconta " +
 "where data < dt_inicial and empresa_id = id_empresa and conta_id = id_conta; " +
 "return saldo_anterior;" +
 "end;" +
 "$BODY$ " +
 "LANGUAGE plpgsql VOLATILE COST 100;" +
 "ALTER FUNCTION saldo_anterior_movimentacao(date, bigint, bigint) OWNER TO postgres;";

transactionFunctionConta.begin();
entityManagerFunctionConta.createNativeQuery(functionConta).executeUpdate();
transactionFunctionConta.commit();

但抛出以下异常

Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.4.1.v20121003-ad44345): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.BatchUpdateException: Entrada em lote 1 <unknown> foi abortada. Chame getNextException para ver a causa.
Error Code: 0
    at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:324)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeJDK12BatchStatement(DatabaseAccessor.java:882)
    at org.eclipse.persistence.internal.databaseaccess.DynamicSQLBatchWritingMechanism.executeBatchedStatements(DynamicSQLBatchWritingMechanism.java:144)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.writesCompleted(DatabaseAccessor.java:1714)

这个功能有问题吗?我们可以通过nativeQuery运行这样的命令吗?

0 个答案:

没有答案