title: How do I return only two fields from two separate tables using Hibernate?
body: <p>How would I return the following JSON using the classes below, <strong>without returning</strong> <code>userId</code> and <code>bookId</code>? I have tried various different queries and none yield the desire result. It doesn't have to match the json names exactly. I just want a username and a list of books to be returned.</p>
  <p>Please refer to Repository <code>@Query</code></p>
  <pre><code>{
     &quot;userName&quot;: &quot;jamesbond007&quot;,
     &quot;books&quot;: [
         &quot;Cat In the Hat&quot;,
         &quot;Green Eggs and Ham
     ]
  }
  </code></pre>
  <p><strong>Users.java:</strong></p>
  <pre><code>@Getter
  @Entity
  @Table(name = &quot;users&quot;)
  public class User {
      @Id
      @OneToMany
      @JoinColumn(name = &quot;user_id&quot;)
      private Integer userId;
      
     @Column(name = &quot;user_name&quot;)
      private String userName;
  }
  </code></pre>
  <p><strong>Books.java:</strong></p>
  <pre><code>@Getter
  @Entity
  @Table(name = &quot;books&quot;)
  public class Books {
      @Id
      @OneToMany
      @JoinColumn(name = &quot;book_id&quot;) 
      private Integer bookId;
  
      @Column(name = &quot;book_name&quot;)
      private String bookName;
  }
  </code></pre>
  <p><strong>UserBooks.java:</strong></p>
  <pre><code>@Getter
  @Entity
  @Table(name = &quot;user_books&quot;)
  public class UserBooks{
      @Id
      @Column(name = &quot;user_book_id&quot;)
      private Integer userBookId;
      
      @ManyToOne
      @JoinColumn(name = &quot;user_id&quot;)
      private User user
      
      @ManyToOne
      @JoinColumn(name = &quot;book_id&quot;)
      private Book book;
  }
  </code></pre>
  <p><strong>Repository:</strong></p>
  <pre><code>public interface MyRepository extends JPARepository&lt;UserBooks, Integer&gt; {
       @Query(&quot;SELECT ub.userName, ub.bookName FROM UserBooks ub JOIN ub.User JOIN ub.Book WHERE userId=?1&quot;) //WHAT DO I DO HERE?
       UserBooks getAllBooksByUser(Integer userId);
  }
  </code></pre>