Spring - 如何在插入另一个实体时自动在数据库中插入一个实体(一对一关系)?

发布于 2025-01-09 10:57:26 字数 962 浏览 1 评论 0原文

软件开发人员大家好,

假设我们有以下场景:我们有一个驱动程序可以注册的系统。 每次注册新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);
    }
}

您会如何 实际上,您认为哪一个是实现前面提到的行为的最佳实践?

  1. 在将Driver插入数据库之前手动创建Car实体并将其分配给驱动程序(在前面提到的代码块中),或者
  2. 使用类似@RepositoryEventHandler之类的东西来拦截当 Driver 实体将被插入数据库并更新它们之间的链接时。
  3. 其他 (...)?

另外,如果我们扩大规模,并且还必须为 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?

  1. 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
  2. Use something like @RepositoryEventHandler to intercept when Driver entities will be inserted in the database and update the link between the two of them there.
  3. 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 Houses can have CleaningTeams and so on...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

二手情话 2025-01-16 10:57:26

首先,您需要根据您的需求设计数据库。如果您想为房屋、汽车等创建表...那么您可以仅保留数据的标识符(如果它是一对一映射或一对多映射)。多对多需要多一张表进行映射。

让我们看看如何保留标识符。
伪代码:

@Entity
class Driver {
   @OneToMany
   List<Car> cars;
   @OneToMany
   List<House> houses;
}


@Entity
class Driver {
   @OneToOne
   Car car;
   @OneToOne
   House house;
}

@Entity
class Driver {
// It can be Car or House it does not matter.
   private String entityIdentifier;
}

我想你可以定义更灵活的方式。只需为汽车、房屋等创建一个表(可能是属性表),然后为汽车、房屋等的属性创建另一个表。
您可以将属性实体保留在驱动程序实体中并使其尽可能简单。

@Entity
class Driver {
   @OneToMany
   List<Propert> property;
}
@Entity
class Property {
   // It can be House, Car, etc
   private String type;
   @OneToMany
   private List<Attribute> attributes;
}

@Entity
class Attribute {
    private String key;
    private String value;
}

通过这种设计,您可以简单地创建具有属性的汽车或房屋,这些属性可以是门或轮胎其他属性。

只需在创建驱动程序时插入,然后向该驱动程序添加属性即可,该属性可以是任何内容。此外,您还需要另一个端点来存储属性并分配给用户。比创建驱动时插入要灵活得多。

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:

@Entity
class Driver {
   @OneToMany
   List<Car> cars;
   @OneToMany
   List<House> houses;
}


@Entity
class Driver {
   @OneToOne
   Car car;
   @OneToOne
   House house;
}

@Entity
class Driver {
// It can be Car or House it does not matter.
   private String entityIdentifier;
}

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.

@Entity
class Driver {
   @OneToMany
   List<Propert> property;
}
@Entity
class Property {
   // It can be House, Car, etc
   private String type;
   @OneToMany
   private List<Attribute> attributes;
}

@Entity
class Attribute {
    private String key;
    private String value;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文