TL;DR I want to prevent the user from overwriting objects in the database, yet allow them submit custom objects.
Hello Everyone,
I am pretty much a noob with Hibernate and Spring. But I have an architectural problem that I was wondering if you could help me. Here is the scenario, the user wants to save a car object. The car object contains various parts. Let's start with the wheel, assume the wheel has 2 properties "wheelId" and "wheelManufacturer"
Code:
Wheel
private String wheelId;
private String wheelManufacturer;
//getters and setters.
Now when the user sends in the car for saving , the wheel can either be one that exists in the database, or a custom wheel. If it is one that exists in the db, I want to use that one, basically preventing the user from overwriting the stored wheel. If it doesn't exists, I want to write it to the db. Here is how I do it now.
Code:
class CarService
....
public Car saveCar( Car car )
{
....
Wheel frontWheel = car.getFrontWheel();
//Does the front wheel exists
if( frontWheel != null )
{
//Get the id of the wheel
String frontWheelId = frontWheel.getWheelId();
if( frontWheelId != null )
{
//the wheel has an id, so it should exist in the db, get that copy
Wheel dbWheel = wheelDao.getById( frontWheelId );
if( dbWheel != null )
{
//we found the db copy of the wheel, set that as the property of the car
car.setFrontWheel( dbWheel );
}
}
....
}
}
Needless to say, this becomes redundant when you consider all the different parts a car can have. I was wondering about how other people solve this problem.
Thanks