Bocky
Bocky

Reputation: 483

JPA unable to use START_OBJECT TOKEN as search key

I am facing some issues in my code when I am trying to get update data that was previously inserted into the database. The error code from 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]"

I have 2 entities called Payment and Order, where Payment holds Orders as a @OnetoOne entity. Payment class:

@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;
    }
}

Order class:

@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;
    }
}

In my controller, I will pass in the payment_id and use it to update the payment entry from UNPAID to PAID status. This is where I get the error stated above.

Service snippet:

@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());
    }

Update: Controller snippet

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

Add is working fine from postman, only update has a problem. Any kind of advice would be great. Thank you!

Upvotes: 0

Views: 465

Answers (1)

Mark Bramnik
Mark Bramnik

Reputation: 42431

JPA has nothing to do with a JSON. JSON is just an input format. The chances are that you're trying to supply some json object via some sort of Controller. This controller implicitly tries to convert an object for you and fails to do so. Usually, it happens because the JSON is of invalid format, or at least cannot be mapped onto object hierarchy. So please carefully examine your JSON and try to see how it should be mapped.

Now, it's hard to tell more from the data that you've posted but it's clear that you supply an object (something enclosed in square brackets) in a place where the engine expects to see an Integer. It's rather some relation mapping inside.

So, as our colleague has already stated, it just happens that the objects that fail during serialization, later on inside the flow, are supposed to be passed to JPA engine for database operation, but this particular problem is not related to JPA.

Upvotes: 1

Related Questions