【问题标题】:Why Spring boot JPA native update is giving PSQLException with postgres Database?为什么 Spring boot JPA 本机更新会在 postgres 数据库中提供 PSQLException?
【发布时间】:2020-05-26 12:19:51
【问题描述】:

我有这个实体类:

@Entity
@Table(name = "inbox_inbox")
@Getter
@Setter
@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
public class Inbox implements Serializable {

    @Id
    private int id;
    @Column(name = "created")
    private Date created;
    @Column(name = "modified")
    private Date modified;
    @Column(name = "status")
    private String status;
}

我有这个仓库:

@Repository
public interface InboxRepository extends JpaRepository<Inbox, Integer> {

    List<Inbox> findInboxesByStatus(String status);

    @Modifying
    @Transactional
    @Query(value = "update inbox_inbox i set i.status = ?2 where i.id = ?1", nativeQuery = true)
    int setInboxStatusById(int id, String status);
}

如果我以所需状态调用findInboxesByStatus(String status),那么它会给出预期的结果。但是当调用setInboxStatusById() 时,它给了我一个例外!

我在这里给出我的呼叫部分:

int updatedRows = inboxRepository.setInboxStatusById(2, "processing");

得到这个异常:

2020-02-10 22:21:57.486 DEBUG 7 --- [main] o.s.orm.jpa.JpaTransactionManager        : Creating new transaction with name [org.springframework.data.jpa.repository.support.SimpleJpaRepository.setInboxStatusById]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
2020-02-10 22:21:57.486 DEBUG 7 --- [main] o.s.orm.jpa.JpaTransactionManager        : Opened new EntityManager [SessionImpl(1663686815<open>)] for JPA transaction
2020-02-10 22:21:57.486 DEBUG 7 --- [main] o.h.e.t.internal.TransactionImpl         : On TransactionImpl creation, JpaCompliance#isJpaTransactionComplianceEnabled == false
2020-02-10 22:21:57.486 DEBUG 7 --- [main] o.h.e.t.internal.TransactionImpl         : begin
2020-02-10 22:21:57.486 DEBUG 7 --- [main] org.postgresql.jdbc.PgConnection         :   setAutoCommit = false
2020-02-10 22:21:57.486 DEBUG 7 --- [main] o.s.orm.jpa.JpaTransactionManager        : Exposing JPA transaction as JDBC [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@10fc01e0]
2020-02-10 22:21:57.487 DEBUG 7 --- [main] org.hibernate.SQL                        : update inbox_inbox i set i.status = ? where i.id = ?
2020-02-10 22:21:57.487 DEBUG 7 --- [main] o.h.engine.jdbc.spi.SqlExceptionHelper   : could not execute statement [n/a]

org.postgresql.util.PSQLException: ERROR: column "i" of relation "inbox_inbox" does not exist
  Position: 26
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2497) ~[postgresql-42.2.8.jar!/:42.2.8]
    at .....

为什么要得到这个?我也在这个网站上搜索了一些解决方案。但似乎,这是一个新线程。于是寻求帮助。提前致谢。

【问题讨论】:

  • 您可以使用类似findInboxById(int id) 的简单spring-data jpa 查询它会给您指定的对象,然后您可以通过设置器index.setStatus("Processing") 更改状态,然后再次保存喜欢indexRepository.save(index)
  • 我猜这是一个错误的设计。 :(

标签: java sql spring postgresql spring-data


【解决方案1】:

不允许在更新查询中使用别名。请使用以下内容:

@Modifying
@Transactional
@Query(value = "update inbox_inbox set status = ?2 where id = ?1", nativeQuery = true)
int setInboxStatusById(int id, String status);

【讨论】:

猜你喜欢
  • 2016-11-01
  • 1970-01-01
  • 2017-03-08
  • 2012-04-15
  • 2018-07-17
  • 2022-08-18
  • 2021-11-18
  • 2019-12-06
  • 1970-01-01
相关资源
最近更新 更多