Spring - 如何在插入另一个实体时自动在数据库中插入一个实体(一对一关系)?
软件开发人员大家好,
假设我们有以下场景:我们有一个驱动程序可以注册的系统。 每次注册新Driver
时,系统都会自动构建并将Car
分配给该驾驶员。
让我们考虑一下我们有以下休息控制器,它公开了驱动程序在系统中注册的端点:
@RestController
@RequestMapping("/api/register")
public class DriverController {
...
@PostMapping
public User register(@RequestParam String email, [...]) {
final Driver driver = new Driver(email);
...
return repository.save(driver);
}
}
您会如何 实际上,您认为哪一个是实现前面提到的行为的最佳实践?
- 在将Driver插入数据库之前手动创建
Car
实体并将其分配给驱动程序(在前面提到的代码块中),或者 - 使用类似
@RepositoryEventHandler
之类的东西来拦截当Driver
实体将被插入数据库并更新它们之间的链接时。 - 其他 (...)?
另外,如果我们扩大规模,并且还必须为 Driver
分配一个 House
和一个 VacationHouse
,会发生什么。现在,汽车
还应该有一个车库
和服务历史记录
,而房子
可以有>清洁团队等等...
Hi fellow software developers,
Let's assume we have the following scenario: we have a system where drivers can register.
Each time a new Driver
registers, the system automatically builds and assigns a Car
to the driver.
Let's consider we have the following rest controller which exposes an endpoint for the drivers to register in the system:
@RestController
@RequestMapping("/api/register")
public class DriverController {
...
@PostMapping
public User register(@RequestParam String email, [...]) {
final Driver driver = new Driver(email);
...
return repository.save(driver);
}
}
How would you Actually which one do you think is the best practice in order to achieve the previously mentioned behavior?
- Manually create the
Car
entity and assign it to the driver before inserting the Driver into the Database (in the previously mentioned block of code), or - Use something like
@RepositoryEventHandler
to intercept whenDriver
entities will be inserted in the database and update the link between the two of them there. - Other (...)?
Also, what happens if we scale up and we have to also assign a House
and a VacationHouse
to the Driver
. And now the Car
should have also a Garage
and a Service History
, while the House
s can have CleaningTeam
s and so on...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您需要根据您的需求设计数据库。如果您想为房屋、汽车等创建表...那么您可以仅保留数据的标识符(如果它是一对一映射或一对多映射)。多对多需要多一张表进行映射。
让我们看看如何保留标识符。
伪代码:
我想你可以定义更灵活的方式。只需为汽车、房屋等创建一个表(可能是属性表),然后为汽车、房屋等的属性创建另一个表。
您可以将属性实体保留在驱动程序实体中并使其尽可能简单。
通过这种设计,您可以简单地创建具有属性的汽车或房屋,这些属性可以是门或轮胎其他属性。
只需在创建驱动程序时插入,然后向该驱动程序添加属性即可,该属性可以是任何内容。此外,您还需要另一个端点来存储属性并分配给用户。比创建驱动时插入要灵活得多。
First of all you need to design your database according to your needs. If you want to create table for House, Car, etc... then you can keep just identifier of data if it is one to one mapping or one to many. Many to Many needs to one more table for mapping.
lets see how you can keep identifier.
Pseudo Code:
I guess you can define much more flexible way. Just create a table(maybe property table) for Car,House, etc and create another table for attributes of Car, House ,etc.
You can keep property entity in Driver entity and keep it as simple as possible.
By this design you can simply create Car or House with attributes which can be doors or tires other attributes.
Just insert when you are creating driver and simply add property to that driver which can be anything. Also you will need another endpoint for property and for assigning to user. It is much more flexible then inserting when creating driver.