Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法将INSERT与从Spring数据存储库返回的本机查询一起使用_Java_Postgresql_Spring Data Jpa - Fatal编程技术网

Java 无法将INSERT与从Spring数据存储库返回的本机查询一起使用

Java 无法将INSERT与从Spring数据存储库返回的本机查询一起使用,java,postgresql,spring-data-jpa,Java,Postgresql,Spring Data Jpa,我在SpringDataJPA存储库中使用本机SQL查询,希望在upsert后返回自动生成的id。 类似于下面的一个片段: public interface EntityRepository extends CrudRepository<Entity, Integer> { @Modifying @Transactional @Query(value = "INSERT INTO entity (name, age) VALUES(?1,?2) " +

我在SpringDataJPA存储库中使用本机SQL查询,希望在upsert后返回自动生成的id。 类似于下面的一个片段:

public interface EntityRepository extends CrudRepository<Entity, Integer> {

    @Modifying
    @Transactional
    @Query(value = "INSERT INTO entity (name, age) VALUES(?1,?2) " +
                       "ON CONFLICT(name) DO UPDATE SET name=?1,age=?2 " +
                       "RETURNING id", nativeQuery = true)
    Integer upsert(String name, int age); 
}
因此,问题在于:

org.postgresql.jdbc.PgPreparedStatement:

public int executeUpdate() throws SQLException {
   executeWithFlags(QueryExecutor.QUERY_NO_RESULTS);

   ResultWrapper iter = result;
   while (iter != null) {
     if (iter.getResultSet() != null) {
        throw new PSQLException(GT.tr("A result was returned when none was expected."),
        PSQLState.TOO_MANY_RESULTS);

     }
   iter = iter.getNext();
  }

return getUpdateCount();
}
如果我可以设置另一个QueryExecutor标志,或者使用
语句调用update语句,那么问题就会解决。返回生成的\u键
。但现在似乎没有办法从Spring数据存储库中指定它。我知道我可以从存储过程中做我想做的事情,但它不如注释中的某些参数那么方便

那么,你认为呢?它应该被认为是Spring数据和Postgres JDBC中的一个bug吗

UPD: 公开发行的Jira-

org.postgresql.jdbc.PgPreparedStatement:

public int executeUpdate() throws SQLException {
   executeWithFlags(QueryExecutor.QUERY_NO_RESULTS);

   ResultWrapper iter = result;
   while (iter != null) {
     if (iter.getResultSet() != null) {
        throw new PSQLException(GT.tr("A result was returned when none was expected."),
        PSQLState.TOO_MANY_RESULTS);

     }
   iter = iter.getNext();
  }

return getUpdateCount();
}