title: How to save an object to db and connect it with logged in user
body: <p>I'm making my first web-app using Spring Boot, Thymeleaf, Hibernate (with in-memory H2 database). I also want to use <strong>Spring Security</strong> but I try to avoid it for now (want to finish Entities and repositories first although I know I will have to change the way they work) I want a user to register on my site (he is created and added to my db) and then he can CRUD his ToDos (which are also saved to db). I have connected both entitites with bidirectional OneToMany using annotations and @JoinTable/@mappedBy. Here are my entities:</p>
  
  <p><strong>ToDoItem.java</strong></p>
  
  <pre><code>@Entity
  @Table (name = "TO_DO_ITEM")
  public class ToDoItem extends BaseEntity {
  
  
  @Column(name = "TITLE", nullable = false)
  private String title;
  
  @Column(name = "COMPLETED")
  private boolean completed;
  
  // TODO: 29.01.17 Add description property
  
  @Column(name = "DUE_DATE", nullable = false)
  private LocalDate dueDate;
  
  // two-directional OneToMany
  @ManyToOne
  @JoinColumn(name = "USER_ID")
  private User user;
  
  
  // JPA demands empty contructor
  ToDoItem() {}
  
  public ToDoItem(String title, LocalDate dueDate) {
      this.title = title;
      this.dueDate = dueDate;
  }
  
  
  public String getTitle() {
      return title;
  }
  
  public void setTitle(String title) {
      this.title = title;
  }
  
  public boolean isCompleted() {
      return completed;
  }
  
  public void setCompleted(boolean completed) {
      this.completed = completed;
  }
  
  public LocalDate getDueDate() {
      return dueDate;
  }
  
  public void setDueDate(LocalDate dueDate) {
      this.dueDate = dueDate;
  }
  
  
  public User getUser() {
      return user;
  }
  
  public void setUser(User user) {
      this.user = user;
  }
  
  }
  </code></pre>
  
  <p><strong>User.java</strong></p>
  
  <pre><code>@Entity
  @Table (name = "USERS")
  public class User extends BaseEntity {
  
  @Column(name = "USERNAME")
  private String username;
  
  // TODO: 28.01.17 Find a way to store hashed and salted pws in DB
  @Column(name = "PASSWORD")
  private String password;
  
  @Column(name = "EMAIL")
  private String email;
  
  @OneToMany(mappedBy = "user")
  private Set&lt;ToDoItem&gt; toDoItems = new HashSet&lt;&gt;();
  
  // JPA demands empty constructor
  User() {}
  
  public User(String username, String password, String email) {
      this.username = username;
      this.password = password;
      this.email = email;
  }
  
  public Set&lt;ToDoItem&gt; getToDoItems() {
      return toDoItems;
  }
  
  public void setToDoItems(Set&lt;ToDoItem&gt; toDoItems) {
      this.toDoItems = toDoItems;
  }
  
  public String getUsername() {
      return username;
  }
  
  public void setUsername(String username) {
      this.username = username;
  }
  
  public String getPassword() {
      return password;
  }
  
  public void setPassword(String password) {
      this.password = password;
  }
  
  public String getEmail() {
      return email;
  }
  
  public void setEmail(String email) {
      this.email = email;
  }
  
  }
  </code></pre>
  
  <p>I am initialized a set in User.java so I can add created items by logged in user to his own set so they are not just sent to the db, but they are actually connected. Now I just need a method to add newly created item by him to his set (and to the db). </p>
  
  <p>I have two Service beans:
  <strong>ToDoItemServiceBean.java</strong></p>
  
  <pre><code>@Service
  public class ToDoItemServiceBean implements ToDoItemService {
  
  @Autowired
  private ToDoItemRepository toDoItemRepository;
  
  @Override
  public Iterable&lt;ToDoItem&gt; listAllToDoItems() {
      return toDoItemRepository.findAll();
  }
  
  @Override
  public ToDoItem getToDoItemById(Long id) {
      return toDoItemRepository.findOne(id);
  }
  
  @Override
  public ToDoItem addNewToDo(ToDoItem toDoItem) {
      return toDoItemRepository.save(toDoItem);
  }
  
  @Override
  public void deleteToDo(Long id) {
      toDoItemRepository.delete(id);
  }
  }
  </code></pre>
  
  <p><strong>UserServiceBean.java</strong></p>
  
  <pre><code>@Service
  public class UserServiceBean implements UserService {
  
  @Autowired
  private UserRepository userRepository;
  
  @Override
  public User saveUser(User user) {
      return userRepository.save(user);
  }
  }
  </code></pre>
  
  <p>And I have no clue how to add those ToDos to a specific user in db. Should I just add an item to a set in addNewToDo so apart from adding new ToDo to db this method would also somehow connect ToDo with a user? But how do I do it? What's the best approach to fix it?</p>
  
  <p><a href="https://github.com/doublemc/ToDoWebApp" rel="nofollow noreferrer">HERE</a> is the rest of my code (didn't want to paste all of my files into this post). </p>
  
  <p>EDIT:
  I rethought my db structure and I think that I should have OneToMany in UserEntity because User can have many ToDos but OneToOne in ToDoItemEntity because a ToDo can have only one user - that's correct, right?</p>
  
  <p>And from that link I made something like this, is it ok?</p>
  
  <p><strong>ToDoItemEntity</strong></p>
  
  <pre><code>// a ToDoItem is only associated with one user
  @OneToOne(cascade = CascadeType.ALL, mappedBy = "toDoItems")
  private User user;
  </code></pre>
  
  <p><strong>UserEntity</strong></p>
  
  <pre><code>//user can have many ToDoItems
  @OneToMany(cascade = CascadeType.ALL)
  @JoinColumn(name = "USER_ID")
  private Set&lt;ToDoItem&gt; toDoItems;
  </code></pre>
  
  <p>Added updateUser to:
  <strong>UserServiceBean.java</strong></p>
  
  <pre><code>@Override
  public void updateUser(ToDoItem toDoItem) {
      User currentUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
      User queriedUser = userRepository.findOne(currentUser.getId());
      queriedUser.setToDoItems();
  }
  </code></pre>