我正在创建一个数据模型,使我可以管理发票。我遇到的问题是当我尝试通过@POST使用服务时。一切都正确,即使我在数据库中进行验证并且没有外键invoice_id的记录
此处错误:
@Entity
@Table(name = "invoice")
data class Invoice (
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Int? = 0,
val createdAt: LocalDateTime? = LocalDateTime.now(),
val updatedAt: LocalDateTime? = LocalDateTime.now(),
val nombre: String? = "",
val direccion: String? = "",
val telefono: String? = "",
var order_total : Int? = null,
@OneToMany(mappedBy = "invoice", cascade = [CascadeType.ALL])
@JsonManagedReference
val invoiceItems: List<InvoiceItem>? = mutableListOf()
)
@Entity
@Table(name = "item")
data class Item(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long? = null,
val name: String? = null,
val description: String? = null,
val price: Int? = null,
val img_url: String? = null,
val createdAt: LocalDateTime? = LocalDateTime.now(),
val updatedAt: LocalDateTime? = LocalDateTime.now(),
@OneToMany(mappedBy = "item", cascade = [CascadeType.ALL])
@JsonBackReference
val items: List<InvoiceItem>? = mutableListOf()
)
@Entity
@Table(name = "invoice_item")
data class InvoiceItem(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Int? = 0,
val quantitiy: Int? = 0,
@ManyToOne
@JoinColumn (name = "invoice_id")
@JsonIgnore
val invoice: Invoice? = null,
@ManyToOne
@JoinColumn
val item: Item? = null
)
HTTP/1.1 200
Content-Type: application/json
Transfer-Encoding: chunked
Date: Sat, 08 Aug 2020 04:35:13 GMT
Keep-Alive: timeout=60
Connection: keep-alive
{
"id": 5,
"createdAt": "2020-08-07T23:35:13.2283331",
"updatedAt": "2020-08-07T23:35:13.2283331",
"nombre": "Z",
"direccion": "Bosquera",
"telefono": "12000",
"order_total": 1000,
"invoiceItems": [
{
"id": 7,
"quantitiy": 10,
"item": {
"id": 1,
"name": "Costilla de Cerdo Ahumadas Ranchera",
"description": "Costilla Ahumada, 500 Gramo(s).",
"price": 30700,
"img_url": "https://images.rappi.com/products/443434-1594427331970.png",
"createdAt": "2020-08-07T23:28:03",
"updatedAt": "2020-08-07T23:28:03"
}
},
{
"id": 8,
"quantitiy": 10,
"item": {
"id": 2,
"name": "Hamburguesa Preasada Zenu X 400G",
"description": "Hamburguesa Preasada Zenu X 400G. 25% Reducido En Sodio, Buena Fuente De Proteína PLU: 802599",
"price": 12350,
"img_url": "https://images.rappi.com/products/802599-1594427485528.png",
"createdAt": "2020-08-07T23:28:03",
"updatedAt": "2020-08-07T23:28:03"
}
}
]
}
我非常感谢您的帮助,我敢于发表,因为我已经尝试了解问题所在,但我找不到它
答案 0 :(得分:0)
您是否检查JSON反序列化是否正确?如果没有@JsonManagedReference / @JsonBackReference,则可能无法在InvoiceItem中设置双向关系
@Entity
@Table(name = "invoice_item")
data class InvoiceItem(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Int? = 0,
val quantitiy: Int? = 0,
@ManyToOne
@JoinColumn (name = "invoice_id")
@JsonBackReference
val invoice: Invoice? = null,
@ManyToOne
@JoinColumn
@JsonManagedReference
val item: Item? = null
)
答案 1 :(得分:0)
对于双向关系,需要同步两端,这意味着您必须设置每个InvoiceItem的发票字段,然后才能设置外键。
fun addOrder(invoice: Invoice): ResponseEntity<Invoice> {
invoice.invoiceItems.forEach { it -> it.invoice= invoice}
ResponseEntity.ok(parentRepository.save(invoice))
}