当前位置: 首页 > 知识库问答 >
问题:

使用@query和@modifying的Spring data JPA PSQLException

晏和风
2023-03-14

使用spring-boot-starter-data-jpa(版本2.0.4.release)和PostgreSQL(版本9.5.10),我一直得到psqlexception:查询没有返回任何结果,尽管我确实使用了@modifying(这里很多人建议的)。按照一些人的建议添加@transactional也没有起到什么作用。分析StackTrace可以看到调用了org.postgresql.jdbc.pgPreparedStatement.executeQuery(),而应该调用org.postgresql.jdbc.pgPreparedStatement.executeUpdate()。我能做什么?

@Repository
public interface Rank extends JpaRepository<RankArticle, String> {

@Modifying
@Query(value = "UPDATE rank SET id=calc.id, shop_id=calc.shop_id, " +
                "score=calc.score, rank = calc.rank FROM " +
                    "(SELECT id, shop_id, SUM(sales) AS score, CAST(NULL AS INT) AS rank " +
                    "FROM " +
                        "(SELECT id, sales, shop_id FROM daily_pod " +
                        "WHERE shop_id = :shopId " +
                        "UNION " +
                        "SELECT id, sales, shop_id FROM weekly_pod " +
                        "WHERE shop_id = :shopId " +
                        "UNION " +
                        "SELECT id, sales, shop_id FROM end_pod " +
                        "WHERE shop_id = :shopId) " +
                    "AS pods " +
                    "GROUP BY id, shop_id ORDER BY score DESC limit :limit) as calc;"
        , nativeQuery = true)
void calcScore(@Param("shopId") Integer shopId,
               @Param("limit") Integer limit);

编辑:添加完整堆栈-跟踪:

[ERROR] 2018-09-20 17:49:10,503 [SimpleAsyncTaskExecutor-1] [d.t.p.v.backend.VerkaufsrangConfig] - Konnte AMQP-Nachricht nicht

共有1个答案

宗鸿博
2023-03-14

通过将语句更改为INSERT into...ON CONFLICT(...),我设法使其工作请更新一个:

@Transactional
public interface Rank extends CrudRepository<RankArticle, Identity> {

    @Modifying
    @Query(value = "INSERT INTO rank" +
                        "(SELECT id, shop_id, SUM(sales) AS score, CAST(NULL AS INT) AS rank " +
                        "FROM " +
                            "(SELECT id, shop_id, sales FROM daily_pod " +
                            "WHERE shop_id = :shopId " +
                            "UNION " +
                            "SELECT id, shop_id, sales FROM weekly_pod " +
                            "WHERE shop_id = :shopId " +
                            "UNION " +
                            "SELECT id, shop_id, sales FROM end_pod " +
                            "WHERE shop_id = :shopId) " +
                        "AS pods " +
                        "GROUP BY id, shop_id ORDER BY score DESC LIMIT :limit) " +
                "ON CONFLICT (id) DO UPDATE " +
                "SET shop_id = EXCLUDED.shop_id, score = EXCLUDED.score, " +
                    "rank_count = EXCLUDED.rank_count;"
        , nativeQuery = true)
void calcScore(@Param("shopId") Integer shopId,
               @Param("limit") Integer limit);

}

 类似资料:
  • 我看过这些链接 如何使用JPA查询将数据插入到DB中?它使用nativeQuery=true 如何在Spring-Data中插入数据库?建议使用内置的save方法(还有一个saveAndFulsh()方法) 有没有办法只使用@query&@modifying(即不使用本机SQL查询&nativequery=true,或save()或saveAndFlush()来完成插入?

  • 为了测试@version注释,我使用以下设置来管理持久性: Spring Boot Starter 1.5.3.发布 Hibernate5.2.10.final Spring Data Envers 1.1.3.Release->Spring Data JPA 1.11.3.Release 测试的数据库: H2 PostgreSQL Mariadb 甲骨文 实体正在使用一个@version注释字段

  • 问题内容: 我想从返回一个整数列表 呼叫 为什么以下不正确? 特别是为什么我会得到“ …未知实体:java.lang.Integer” 我是否必须创建一个实体类,该实体类具有一个作为Integer的单个字段? 谢谢 问题答案: 您所做的称为 投影 。那是当您仅返回属于一个实体的 标 量值时。您可以使用JPA做到这一点。请参阅标量值。 我认为在这种情况下,可以完全省略实体类型: 例子取自这里。

  • If you are writing a dynamic web page where content containing mathematics may appear after MathJax has already typeset the rest of the page, then you will need to tell MathJax to look for mathematics

  • Example 38. Declare query at the method using the @Query annotation.(使用@Query注解声明查询) public interface BookRepository extends ElasticsearchRepository<Book, String> { @Query("{"bool" : {"must" : {"f