Java 无法将INSERT与从Spring数据存储库返回的本机查询一起使用
我在SpringDataJPA存储库中使用本机SQL查询,希望在upsert后返回自动生成的id。 类似于下面的一个片段: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) " +
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();
}