I have two entities in my Spring Boot project that uses Spring JPA and a MySQL database with Hibernate. These two entities are related to a One to One relationship. The problem is that when I try to add a type of objects, the primary key of the other related object is not migrated.
This is one of my entities:
@Entity
@DynamicUpdate
@Table(name = "tank")
public class Tank {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "level_sensor_id")
private LevelSensor levelSensor;
public LevelSensor getLevelSensor() {
return levelSensor;
}
public void setLevelSensor(LevelSensor levelSensor) {
this.levelSensor = levelSensor;
}
}
And this is the other entity:
@Entity
@DynamicUpdate
@Table(name = "level_sensor")
public class LevelSensor {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@OneToOne(mappedBy="levelSensor", cascade=CascadeType.PERSIST)
private Tank tank;
}
I am trying to add a new LevelSensor to an existing Tank. For this I have the following driver:
@RequestMapping(value="/levelSensor/", method = RequestMethod.POST, produces = "application/json")
@ResponseBody
public ResponseEntity<Object> addLevelSensor(
@RequestBody LevelSensorDTO levelSensorDTO,
@RequestHeader("Accept") String acceptHeader, HttpSession session) {
LevelSensor newLevelSensor= new LevelSensor();
Tank tank = tankService.getTankById(levelSensorDTO.getTankId());
newLevelSensor.setTank(tank);
newLevelSensor = levelSensorService.saveLevelSensor(newLevelSensor);
log.info("The level sensor : "+ levelSensorDTO.toString() +" was successfully added");
return new ResponseEntity<Object>(null ,HttpStatus.OK);
}
Even though I tell the LevelSensor what its tank is, it does not fill the external relationship in the Tank, so it creates the LevelSensor but it is not related to the Tank.
It can be corrected, for this, what I can do is, once the level sensor is added, I can edit the tank and assign it the newly created level sensor. However it is a solution that I do not like and that I do not understand since then I have to edit two objects when I think that editing one should be enough.
Could you help me out or advise me? Could it be that the relationship is wrong?
Thank you very much in advance.
Javier.