【发布时间】:2020-11-28 09:23:39
【问题描述】:
我正在创建一个允许我管理发票的数据模型。我遇到的问题是当我尝试通过@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"
}
}
]
}
非常感谢您的帮助,我敢于发布,因为我已经尝试了解问题所在但我找不到它
【问题讨论】:
-
您可以将您的源代码与此处的示例进行比较,它具有所有类型的数据库关系:github.com/dineshbhagat/spring-boot-web-jpa
-
我刚查了一下,但找不到这种类型的 ManyToMany。
-
你能告诉我你是如何为你的帖子请求编码的吗?
-
我有所有的信息,发布请求,发布响应,以及所有库存物品 -> pastebin.com/v15UKH3K
-
我的意思是您的帖子请求的控制器/服务
标签: spring spring-boot kotlin jpa spring-data-jpa