spring data jpa hibernate-entitymanager.executeupdate或spring repository在使用逐个查询/更新多模式中的实体时都失败

xdnvmnnf  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(215)

我们有多模式数据库,每个模式都包含算法执行表,首先我得到模式列表,然后在循环中为每个模式名准备一个一个的更新查询,然后运行entitymanager.executeupdate(),失败如下错误。不确定什么是根本原因,因为表存在于架构中,我可以使用pgsql客户机对表运行相同的更新查询。
编辑:
尝试使用spring数据存储库查询/更新实体,但也失败了。我们不能在for循环中逐个查询每个模式上的表吗?在运行查询之前,我们正在上下文中指定模式。
失败的方法-
代码:

private void updateJobStatus(EntityManager entityManager, String schemaName, AlgorithmExecution execution) throws Exception{
        try {
    private static final String UPDATE_JOB_STATUS_QUERY =   

            String updateQuery = "UPDATE "+schemaName+".algorithm_execution SET job_status = :job_status, modification_time = :modification_time, error_message = :error_message, job_logs = :job_logs WHERE id = :id";
            EntityTransaction transaction = entityManager.getTransaction();
            transaction.begin();
            entityManager.createNativeQuery(updateQuery)
                    .setParameter("job_status", execution.getJobStatus())
                    .setParameter("modification_time", execution.getModifiedTime())
                    .setParameter("error_message", execution.getErrorMessage())
                    .setParameter("job_logs", execution.getJobLogs())
                    .setParameter("id", execution.getId())
                    .executeUpdate();
            transaction.commit();
        } catch ( Exception ex) {
            LOGGER.warn("An exception occured during updateJobStatus, error {}", ex.getMessage());
            LOGGER.debug("An exception occured during updateJobStatus",ex);
            throw new Exception("An exception occured during updateJobStatus", ex);
        }
    }

下面的hibernate内部日志更新日志中的查询:2020-12-15 12:11:28.159+0000[main][debug]-org.hibernate.sql[org.hibernate.engine.jdbc.spi.sqlstatementlogger:logstatement:128]-更新算法\执行集算法\校验和=?,算法\ id=?,创建\时间=?,错误\消息=?,作业\日志=?,作业\名称=?,作业\状态=?,作业\摘要=?,作业类型=?,修改时间=?,下一次执行时间=?,所有者=?,上一次执行时间=?,开始时间=?,租户id=?其中id=?
下面是异常堆栈跟踪-
例外情况:

ava.lang.Exception: An exception occured during updateJobStatus
    at com.bmc.aif.jobmanager.jobmanagement.AIFJobManagementTask.updateJobStatus(AIFJobManagementTask.java:144)
    at com.bmc.aif.jobmanager.jobmanagement.AIFJobManagementTask.jobStatusFailedOrUnknown(AIFJobManagementTask.java:118)
    at com.bmc.aif.jobmanager.jobmanagement.AIFJobManagementTask.executeTasks(AIFJobManagementTask.java:79)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:389)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:333)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:157)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:416)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1788)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:895)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at com.bmc.aif.jobmanager.jobmanagement.AIFJobManagementTask.main(AIFJobManagementTask.java:149)
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute statement
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188)
    at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1356)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1339)
    at org.hibernate.query.internal.NativeQueryImpl.beforeQuery(NativeQueryImpl.java:264)
    at org.hibernate.query.internal.AbstractProducedQuery.executeUpdate(AbstractProducedQuery.java:1610)
    at com.bmc.aif.jobmanager.jobmanagement.AIFJobManagementTask.updateJobStatus(AIFJobManagementTask.java:139)
    ... 25 common frames omitted
Caused by: org.hibernate.exception.SQLGrammarException: could not execute statement
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:103)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:200)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3430)
    at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:3292)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3704)
    at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:201)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:604)
    at org.hibernate.engine.spi.ActionQueue.lambda$executeActions$1(ActionQueue.java:478)
    at java.base/java.util.LinkedHashMap.forEach(LinkedHashMap.java:684)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:475)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:348)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:40)
    at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:102)
    at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1352)
    ... 29 common frames omitted
Caused by: org.postgresql.util.PSQLException: ERROR: relation "algorithm_execution" does not exist
  Position: 8
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2532)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2267)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:312)
    at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:448)
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:369)
    at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:153)
    at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:119)
    at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61)
    at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:197)
    ... 41 common frames omitted

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题