4

I have an entity with two columns refering same column in other table. Basically, a Transaction depends on Account: when creating a new transaction a send money from one account to another.

Account:

@Entity
@Table(name = "accounts")
public class Account implements java.io.Serializable {

    private static final long serialVersionUID = 2612578813518671670L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "idaccount", unique = true, nullable = false)
    private Long idaccount;

    @Column(name = "name", length = 50)
    private String name;

    @NotNull
    @ManyToOne
    @JoinColumn(name = "iduser")
    private User user;

...

Transaction:

@Entity
@Table(name = "transactions")
public class Transaction {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "idtransaction", unique = true, nullable = false)
    private Long idtransaction;

    private BigDecimal amount;

    @NotNull
    @ManyToOne
    @JoinColumn(name = "SOURCE_ACCOUNT")
    private Account sourceAccount;

    @NotNull
    @ManyToOne
    @JoinColumn(name = "TARGET_ACCOUNT")
    private Account targetAccount;

...

TransactionController

@CrossOrigin
@RestController
@RequestMapping("/transaction")
public class TransactionController {

    @Autowired
    TransactionService transactionService;

    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<Transaction> addTransaction(@RequestBody Transaction Transaction) {
        transactionService.save(Transaction);

        return new ResponseEntity<Transaction>(Transaction, HttpStatus.CREATED);
    }

...

If I try post to create a transaction (naturally I have the accounts already created):

{
"amount": 111,
"sourceAccount": 1,
"targetAccount": 2
}

I get:

Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not construct instance of com.mycompany.basicbank.model.Account: no int/Int-argument constructor/factory method to deserialize from Number value (1)
 at [Source: java.io.PushbackInputStream@63447acf; line: 3, column: 18] (through reference chain: com.mycompany.basicbank.model.Transaction["sourceAccount"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.mycompany.basicbank.model.Account: no int/Int-argument constructor/factory method to deserialize from Number value (1)
 at [Source: java.io.PushbackInputStream@63447acf; line: 3, column: 18] (through reference chain: com.mycompany.basicbank.model.Transaction["sourceAccount"])

So my question is: what should I check in order to fix "Can not construct instance of com.livingit.basicbank.model.Account: no int/Int-argument constructor/factory method to deserialize from Number"?

1
  • An error in JSON is nothing ... at all ... to do with the JPA API. They serve utterly different roles. Commented Feb 4, 2018 at 13:59

1 Answer 1

1

The problem is the json you are sending doesn't exactly match the Transaction class. hence you see the error.
But what you are trying to achieve is a valid scenario and can be done.

Some options.

  1. Create a new class(not Transaction) which matches the json. Like

    class TransactionClient {
       BigDecimal amount,
       Long sourceAccount,
       Long targetAccount
    }
    

And in the backend(controller or some in service) you can get the Acccounts from database with this sourceAccount and targetAccount and create a transaction object with this objects and save.

  1. From the frontend call backend to get the Accounts(json) for these source and target accounts and then call your transaction endpoint with the json like this

    {
      "amount": 111,
      "sourceAccount":  {
        "idaccount" :123123,
         ..... // All the Non Null fields
       },
      "targetAccount": {
        "idaccount" :45554,
         ..... // All the Non Null fields
       },
     }
    
Sign up to request clarification or add additional context in comments.

1 Comment

Also you can create your own JsonDeserializer<Transaction> and do it more flexible (Just third way to handle this situration)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.