【发布时间】:2018-02-04 11:19:49
【问题描述】:
我有一个实体,其中两列引用其他表中的同一列。基本上,交易取决于账户:在创建新交易时,从一个账户向另一个账户汇款。
帐号:
@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;
...
交易:
@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;
...
事务控制器
@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);
}
...
如果我尝试发布交易来创建交易(当然我已经创建了帐户):
{
"amount": 111,
"sourceAccount": 1,
"targetAccount": 2
}
我明白了:
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"])
所以我的问题是:我应该检查什么以修复“无法构造 com.livingit.basicbank.model.Account 的实例:没有从 Number 反序列化的 int/Int-argument 构造函数/工厂方法”?
【问题讨论】:
-
JSON 中的错误与 JPA API 无关。他们扮演着完全不同的角色。
标签: spring hibernate jpa spring-data-jpa