title: How to get foreign key of OneToMany relationship in JSP
body: <p>I have two object that have OneToMany relationship, The foreign key is <code>userName</code>. When I check database, I can see the foreign key in there. But I have no idea why I receive nothing.</p>
  
  <p>JSP </p>
  
  <pre><code>//ERROR HERE, I CAN'T GET DATA OF THE FOREIGN KEY 'USERNAME'
  &lt;div class="row"&gt;
      &lt;label class="col-sm-2"&gt;Owner&lt;/label&gt;
      &lt;div class="col-sm-10"&gt;${book.myUserAccount.userName}&lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div class="row"&gt;
      &lt;label class="col-sm-2"&gt;Book's Name&lt;/label&gt;
      &lt;div class="col-sm-10"&gt;${book.tensach}&lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div class="row"&gt;
      &lt;label class="col-sm-2"&gt;Author&lt;/label&gt;
      &lt;div class="col-sm-10"&gt;${book.tacgia}&lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div class="row"&gt;
      &lt;label class="col-sm-2"&gt;Review&lt;/label&gt;
      &lt;div class="col-sm-10"&gt;${book.nhanxet}&lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Model MyUserAccount</p>
  
  <pre><code>public class MyUserAccount implements Serializable {
  
  private static final long serialVersionUID = 1L;
  public static final String ROLE_USER = "ROLE_USER";
  private String id;
  private String email;
  private String userName;
  private String firstName;
  private String lastName;
  private String password;
  private String role;
  private String enabled;
  private List&lt;Book&gt; book = new ArrayList&lt;Book&gt;(0);
  private List&lt;BookTrade&gt; bookTrade = new ArrayList&lt;BookTrade&gt;(0);
  
  public MyUserAccount() {
  
  }
  
  @OneToMany(fetch = FetchType.EAGER, mappedBy = "myUserAccount")
  public List&lt;Book&gt; getBook() {
      return book;
  }
  
  public void setBook(List&lt;Book&gt; book) {
      this.book = book;
  }
  
  @OneToMany(fetch = FetchType.EAGER, mappedBy = "myUserAccount")
  public List&lt;BookTrade&gt; getBookTrade() {
      return bookTrade;
  }
  
  public void setBookTrade(List&lt;BookTrade&gt; bookTrade) {
      this.bookTrade = bookTrade;
  }
  
  public MyUserAccount(String id, String email, String userName, String firstName, //
          String lastName, String password, String role, String enabled) {
      this.id = id;
      this.email = email;
      this.userName = userName;
      this.firstName = firstName;
      this.lastName = lastName;
      this.password = password;
      this.role = role;
      this.enabled = enabled;
  }
  
  @Column(name = "ID", unique = true, nullable = false)
  public String getId() {
      return id;
  }
  </code></pre>
  
  <p>Model Book</p>
  
  <pre><code>public class Book implements Serializable {
  
  private static final long serialVersionUID = 1L;
  private Integer book_ID;
  private String tensach;
  private String tacgia;
  private String nhanxet;
  private String tinhtrang;
  private String theloai;
  private byte[] image;
  private String image_name;
  private String data;
  private MyUserAccount myUserAccount;
  
  public Book() {
  
  }
  
  @ManyToOne(fetch = FetchType.EAGER)
  @JoinColumn(name = "USER_NAME", nullable = false)
  public MyUserAccount getMyUserAccount() {
      return this.myUserAccount;
  }
  
  public void setMyUserAccount(MyUserAccount myUserAccount) {
      this.myUserAccount = myUserAccount;
  }
  
  @Id
  @GeneratedValue(strategy = IDENTITY)
  @Column(name = "book_id", unique = true, nullable = false)
  public Integer getBook_ID() {
      return book_ID;
  }
  </code></pre>
  
  <p>How I mapped two object:</p>
  
  <p>Controller</p>
  
  <pre><code>public String saveBook(@ModelAttribute("bookForm") @Validated Book book, BindingResult result, Model model,
          @RequestParam CommonsMultipartFile[] image, @RequestParam String userName, final RedirectAttributes redirectAttributes)
          throws IOException, UnsupportedEncodingException {
  
      MyUserAccount myUserAccount = myUserAccountDAO.findByUserName(userName);
      book.setMyUserAccount(myUserAccount);
  
      redirectAttributes.addFlashAttribute("css", "success");
      if (book.getBook_ID() == null) {
          System.out.println(book.getBook_ID());
          redirectAttributes.addFlashAttribute("msg", "book added successfully!");
      } else {
          redirectAttributes.addFlashAttribute("msg", "book updated successfully!");
      }
  
      for (CommonsMultipartFile aFile : image) {
          System.out.println("Saving file: " + aFile.getOriginalFilename());
          book.setImage_name(aFile.getOriginalFilename());
          book.setImage(aFile.getBytes());
          System.out.println("Damn that Shit!");
      }
      bookService.saveOrUpdate(book);
      // POST/REDIRECT/GET
      return "redirect:/motsach/" + book.getBook_ID();
  }
  </code></pre>
  
  <p>DAO</p>
  
  <pre><code>public void save(Book book) {
      // TODO Auto-generated method stub
      KeyHolder keyHolder = new GeneratedKeyHolder();
      String sql = "INSERT INTO Books(TENSACH, TACGIA, NHANXET, TINHTRANG, THELOAI, IMAGE, IMAGE_NAME, USER_NAME) "
              + "VALUES ( :tensach, :tacgia, :nhanxet, :tinhtrang, :theloai, :image, :image_name, :user_name)";
  
      namedParameterJdbcTemplate.update(sql, getSqlParameterByModel(book), keyHolder);
      book.setBook_ID(keyHolder.getKey().intValue());
  }
  
  private SqlParameterSource getSqlParameterByModel(Book book) {
      MapSqlParameterSource paramSource = new MapSqlParameterSource();
      paramSource.addValue("book_id", book.getBook_ID());
      paramSource.addValue("tensach", book.getTensach());
      paramSource.addValue("tacgia", book.getTacgia());
      paramSource.addValue("nhanxet", book.getNhanxet());
      paramSource.addValue("tinhtrang", book.getTinhtrang());
      paramSource.addValue("image", book.getImage());
      paramSource.addValue("image_name", book.getImage_name());
      paramSource.addValue("theloai", book.getTheloai());
      paramSource.addValue("user_name", book.getMyUserAccount().getUserName());
      return paramSource;
  }
  </code></pre>