Skip to content
Advertisement

Spring – how to automatically insert an entity in the database when inserting another (One-To-One relationship)?

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…

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement