title: Updating creates new child object instead of updating the existing one Hibernate
body: <p>In my spring boot app I have a Workout object with a list of Exercise objects. The problem is when I try to update Workout, the Exercise that belongs to the Workout is not being updated. Instead it’s creating new Exercise objects in the database. I've added my Workout Entity and the Exercise Entity Below. Any help would be greatly appreciated.</p>
  
  <p>Workout Class:</p>
  
  <pre><code>@Entity
  public class Workout {
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
  
  
  @OneToMany(cascade = CascadeType.ALL)
  @JoinColumn(name = "workout_id")
  private List&lt;Exercise&gt; exercises;
  
  public Workout(List&lt;Exercise&gt; exercises) {
      this.exercises = exercises;
  }
  
  public Workout() {
  }
  
  public Long getId() {
      return id;
  }
  
  public void setId(Long id) {
      this.id = id;
  }
  
  public List&lt;Exercise&gt; getExercises() {
      return exercises;
  }
  
  public void setExercises(List&lt;Exercise&gt; exercises) {
      this.exercises = exercises;
  }
  }
  </code></pre>
  
  <p>Exercise Class:</p>
  
  <pre><code>@Entity
  @Table
  public class Exercise {
  
  @Id
  @GeneratedValue
  private Long id;
  
  
  private String exerciseName;
  private int weight;
  private int actualReps;
  
  @ManyToOne
  @JoinColumn(name = "workout_id")
  private Workout workout;
  
  public Exercise(){}
  
  
  public Exercise( String exerciseName, int weight, int actualReps) {
      this.exerciseName = exerciseName;
      this.weight = weight;
      this.actualReps = actualReps;
  }
  
  public Long getId() {
      return id;
  }
  
  public void setId(Long id) {
      this.id = id;
  }
  
  public String getExerciseName() {
      return exerciseName;
  }
  
  public void setExerciseName(String exerciseName) {
      this.exerciseName = exerciseName;
  }
  
  public int getWeight() {
      return weight;
  }
  
  public void setWeight(int weight) {
      this.weight = weight;
  }
  
  public int getActualReps() {
      return actualReps;
  }
  
  public void setActualReps(int actualReps) {
      this.actualReps = actualReps;
  }
  }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>@Controller
  public class WorkoutController {
  
  @Autowired
  private WorkoutService workoutService;
  @Autowired
  private OneRepMaxService oneRepMaxService;
  @Autowired
  private ActualRepsService actualRepsService;
  @Autowired
  private PercentageService percentageService;
  @Autowired
  private ExerciseService exerciseService;
  
  
  
  @RequestMapping("/")
  @SuppressWarnings("unchecked")
  public String showWorkout(Long id, Model model,Workout workout){
  
  
  
  
  
      Workout firstWorkout = workoutService.findById(Long.valueOf(83));
  
      model.addAttribute("firstWorkout",firstWorkout);
      model.addAttribute("workout",workout);
  
      return "workout";
  }
  
  @RequestMapping(value="/workoutLogs", method = RequestMethod.POST)
  public String saveWorkout(@ModelAttribute Workout workout,Model model) {
  
  
      workoutService.saveNewWorkout(workout);
  
  
      return "redirect:/workoutLogs";
  }
  
  
  @RequestMapping("/workoutLogs")
  public String showAllSavedWorkoutLogs(Model model){
  
  
      return "WorkoutLogs";
  
  }
  
  
  
  }
  </code></pre>
  
  <p>WorkoutDao:</p>
  
  <pre><code>@Repository
  public class WorkoutDaoImpl implements WorkoutDao {
  
  @Autowired
  SessionFactory sessionFactory;
  
  @Override
  @SuppressWarnings("unchecked")
  public List&lt;Workout&gt; findAll() {
      Session session = sessionFactory.openSession();
      List&lt;Workout&gt; workouts = session.createCriteria(Workout.class).list();
      session.close();
      return workouts;
  }
  
  @Override
  public Workout getFirstWorkoutExerciseName(Long id) {
      return null;
  }
  
  @Override
  public Workout getWorkout() {
      return null;
  
  
  }
  
  
  @Override
  public Workout findById(Long id) {
      Session session = sessionFactory.openSession();
      Workout workout = session.get(Workout.class,id);
      Hibernate.initialize(Workout.class);
      return workout;
  }
  
  
  @Override
  public void save(Workout workout) {
      Session session = sessionFactory.openSession();
  
      // Begin a transaction
      session.beginTransaction();
  
      // Save the category
      session.saveOrUpdate(workout);
  
      // Commit the transaction
      session.getTransaction().commit();
  
      // Close the session
      session.close();
  }
  
  @Override
  public void delete(Workout workout) {
  
  }
  
  
  }
  </code></pre>