org.postgresql.util.PSQLException:插入 SQL 时查询没有返回结果

问题描述 投票:0回答:1

我使用下面的代码来执行本机查询:

@Entity
@Table(name = "users")
@Getter
@Setter
public class Users{

    @Id
    @SequenceGenerator(name = "cons_seq", sequenceName = "cons_seq", allocationSize = 100)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "cons_seq")
    @Column(name = "id", nullable = false)
    private Long id;

    @Column(name = "created_by")
    private String created_by;
}

存储库:

@Repository
public interface ProfileRepository extends JpaRepository<Users, Long> {

  @Query(value = """
                    INSERT INTO users (id, created_by)
                    VALUES (nextval('cons_seq'), :createdBy)
            """, nativeQuery = true)
  void create(@Param("createdBy") String createdBy);
}

.......

public void create(final Profile profile) {
    profileRepository.create("test);
}

我收到错误:

Caused by: org.postgresql.util.PSQLException: No results were returned by the query.

记录已添加到数据库中,但出现错误。你知道我该如何解决这个问题吗?

postgresql spring-boot spring-data-jpa
1个回答
0
投票

您需要使用修改注释来指定您的存储库方法正在修改数据。

还将服务或存储库标记为事务性。

@Query(value = """
                INSERT INTO users (id, created_by)
                VALUES (nextval('cons_seq'), :createdBy)
        """, nativeQuery = true)
@Modifying
void create(@Param("createdBy") String createdBy);
© www.soinside.com 2019 - 2024. All rights reserved.