gpt4 book ai didi

java - JPA 无法使用 START_OBJECT TOKEN 作为搜索键

转载 作者:行者123 更新时间:2023-12-02 12:11:35 24 4
gpt4 key购买 nike

当我尝试获取之前插入数据库的更新数据时,我的代码中遇到了一些问题。 postman 的错误代码:

"JSON parse error: Can not deserialize instance of java.lang.Integer out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.Integer out of START_OBJECT token\n at [Source: java.io.PushbackInputStream@647f9992; line: 1, column: 1]"

我有 2 个名为 Payment 和 Order 的实体,其中 Payment 将 Orders 作为 @OnetoOne 实体保存。付款类别:

@Entity
@Table(name = "tbl_payment")
public class Payment {

@Id
@GeneratedValue
@Column(name = "payment_id")
Integer id;
@OneToOne(cascade=CascadeType.ALL)
private Order order;
@Column(name = "payment_receivable")
private double receivable;
@Column(name = "payment_outstanding")
private double outstanding;
@Column(name = "payment_status")
private String status;

public Payment(Order order, double receivable, double outstanding, String status) {
this.order = order;
this.receivable = receivable;
this.outstanding = outstanding;
this.status = status;
}

public Payment() {
}

@Override
public String toString() {
return "Payment{" +
"id=" + id +
", order=" + order +
", receivable=" + receivable +
", outstanding=" + outstanding +
", status='" + status + '\'' +
'}';
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public Order getOrder() {
return order;
}

public void setOrder(Order order) {
this.order = order;
}

public double getReceivable() {
return receivable;
}

public void setReceivable(double receivable) {
this.receivable = receivable;
}

public double getOutstanding() {
return outstanding;
}

public void setOutstanding(double outstanding) {
this.outstanding = outstanding;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}
}

订单类别:

@Entity
@Table(name = "tbl_order")
public class Order {

@Id
@GeneratedValue
@Column(name = "order_id")
Integer id;
@Column(name = "order_amount")
int amount;
@Column(name = "sale_id")
String sale_id;

public Order(int amount, String sale_id) {
this.amount = amount;
this.sale_id = sale_id;
}

public Order() {
}

@Override
public String toString() {
return "Order{" +
"id=" + id +
", amount=" + amount +
", sale_id='" + sale_id + '\'' +
'}';
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public int getAmount() {
return amount;
}

public void setAmount(int amount) {
this.amount = amount;
}

public String getSale_id() {
return sale_id;
}

public void setSale_id(String sale_id) {
this.sale_id = sale_id;
}
}

在我的 Controller 中,我将传入 payment_id 并使用它来将付款条目从未付状态更新为已付状态。这就是我收到上述错误的地方。

服务片段:

@Transactional
@Modifying
public Payment clearPaymentStatus(Integer p){
TypedQuery query = em.createQuery("update Payment p set p.payment_status = ?1 where p.id = ?5", Payment.class);
query.setParameter(1, "PAID");
query.setParameter(5, new Integer(p));
return ((Payment) query.getSingleResult());
}

更新: Controller 片段

@RequestMapping("/clearpayment")
public @ResponseBody ResponseEntity<Payment> ClearPayment(@RequestBody Integer payment_id){
return new ResponseEntity<Payment>( paymentService.clearPaymentStatus(payment_id), HttpStatus.ACCEPTED);
}

postman 的添加工作正常,只有更新有问题。任何建议都会很棒。谢谢!

最佳答案

JPA 与 JSON 无关。 JSON 只是一种输入格式。您可能正在尝试通过某种 Controller 提供一些 json 对象。该 Controller 隐式尝试为您转换对象,但失败了。通常,发生这种情况是因为 JSON 格式无效,或者至少无法映射到对象层次结构。因此,请仔细检查您的 JSON 并尝试了解应如何映射它。

现在,很难从您发布的数据中得知更多信息,但很明显您在引擎期望看到整数的位置提供了一个对象(方括号内的对象)。它内部有一些关系映射。

因此,正如我们的同事已经说过的,恰好序列化期间失败的对象,稍后在流程中,应该传递给 JPA 引擎进行数据库操作,但这个特定问题与 JPA 无关.

关于java - JPA 无法使用 START_OBJECT TOKEN 作为搜索键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46509365/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com