gpt4 book ai didi

spring - Spring数据存储库不会以多对多关系保存数据

转载 作者:行者123 更新时间:2023-12-02 13:41:14 26 4
gpt4 key购买 nike

我正在创建一个数据模型,使我可以管理发票。我遇到的问题是当我尝试通过@POST使用服务时。一切都正确,即使我在数据库中进行验证并且没有外键invoice_id的记录
错误在这里:
enter image description here
enter image description here

@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"
}
}
]
}

非常感谢您的帮助,我敢于发表,因为我已经尝试了解问题所在,但我找不到它

最佳答案

对于双向关系,需要同步两端,这意味着必须设置每个InvoiceItem的发票字段,然后才能设置外键。

fun addOrder(invoice: Invoice): ResponseEntity<Invoice> {
invoice.invoiceItems.forEach { it -> it.invoice= invoice}
ResponseEntity.ok(parentRepository.save(invoice))
}

关于spring - Spring数据存储库不会以多对多关系保存数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63311963/

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