Dealing with JPA relationships in hexagonal architecture

268 Views Asked by At

I'm new to hexagonal architecture & ran into a scenario where I can't figure how it can be achieved. I have two domain objects say Employee & Vehicle. Employee can have many vehicles. My Employee domain object has the List of Vehicles. Similarly, I have the EployeeRepository & VehicleRepository classes which are used by respective adapters (implementation of ports).

The scenario is as below.

  • The vehicles are saved first without any reference to Employee
  • The Employee object is then created (based on certain grouping, the vehicles are then linked to employee)
  • The Employee & vehicles are linked.

This is what I'm doing.

  • Save the Vehicle objects
  • Try to save the Employee object

So in my application service, I get the vehicle objects first & then map them to domain objects. Now I group the vehicles & link Employee & vehicles. The call is then transferred to Employee adapter for saving. But while saving, I would need the vehicle entity reference from db so as to correctly associate the Employee & Vehicles. Which means I need to make another db call from Employee adapter & then save. While I already had the vehicle references in my service class but the state is lost as I'm converting them to domain objects. This should be a common scenario in hexagonal architecture where the app layer only deals with domain objects & hence the entity representation is lost.

Can any one please help what I'm missing here.

0

There are 0 best solutions below