title: Insert users id to the patient in Spring Boot
body: <p>I can't insert the id of the currently logged-in user while making the patient data.
  I want the user to be able to add his own patients, but the problem is that when I add a new patient, the column id_user is null</p>
  <p>I tried lots of ways but couldn't add id_user to the patient.
  What do I miss?</p>
  <p>This is my User Entity:</p>
  <pre><code>    @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private int idUser;
      private String firstName;
      private String lastName;
      private String username;
      ...
  
      @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
      @JoinTable(name = &quot;users_roles&quot;,
              joinColumns = @JoinColumn(name = &quot;id_user&quot;),
              inverseJoinColumns = @JoinColumn(name = &quot;id_role&quot;))
      @JsonManagedReference
      private Set&lt;Role&gt; roles = new HashSet&lt;&gt;();
  
      @OneToMany(cascade = CascadeType.ALL, mappedBy = &quot;user&quot;)
      private List&lt;Patient&gt; patients = new ArrayList&lt;&gt;();
  
      public void add(Patient patient) {
          if (patient != null) {
              if (patients == null) {
                  patients = new ArrayList&lt;&gt;();
              }
          patients.add(patient);
          patient.setUser(this);
          }
      }
  </code></pre>
  <p>Patient Entity:</p>
  <pre><code>   @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = &quot;id_patient&quot;)
      private int idPatient;
  
      @Column(name = &quot;first_name&quot;)
      private String firstName;
      @Column(name = &quot;last_name&quot;)
      private String lastName;
  ...
  
      @ManyToOne
      @JoinColumn(name = &quot;user_id&quot;, nullable = false)
      private User user;    
  }
  </code></pre>
  <p>Controller</p>
  <pre><code> @RequestMapping(&quot;/addPatient&quot;)
      public String addPatient(Model theModel, HttpServletRequest request) {
  
          Authentication auth = SecurityContextHolder.getContext().getAuthentication();
          UserDetails userD = (UserDetails) auth.getPrincipal();
          User u = userService.findByUsername(userD.getUsername());
  
          request.getSession().setAttribute(&quot;id_user&quot;, u.getIdUser());
  //        int userId = user.getIdUser();
          int userId = (int) request.getSession().getAttribute(&quot;id_user&quot;);
  
          User user = new User();
          user.setIdUser(userId);
          
          Patient patient = new Patient();
          patient.setUser(user);
  
          theModel.addAttribute(&quot;patient&quot;, patient);
  
          return &quot;user/patients/add-patient-dashboard&quot;;
      }
  
   @PostMapping(&quot;savePatient&quot;)
      public String savePatient(@ModelAttribute(&quot;patient&quot;) Patient thePatient, Model model) {
          patientService.save(thePatient);
          return &quot;redirect:/user/allPatients&quot;;
      }
  </code></pre>
  <p>I try editing the User service from:</p>
  <pre><code>@Override
      public void save(User user) {
          user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
          userRepository.save(user);
      }
  </code></pre>
  <p>to:</p>
  <pre><code>@Override
      public void save(User user) {
          List&lt;Patient&gt; patients = user.getPatients();
          patients.forEach(patient -&gt; user.add(patient));
          user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
          userRepository.save(user);
      }
  </code></pre>
  <p><strong>if someone can help me resolve this I would appreciate it since I'm struggling for a very long time with this</strong></p>
  <p><a href="https://i.stack.imgur.com/sy2l7.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sy2l7.jpg" alt="enter image description here" /></a></p>