title: JOIN two tables JPQL
body: <pre><code>@Entity
  @Table(name="tblUser")    
  public class User {
  
  @Id
  @GeneratedValue
  @Column(name="id")
  private Long id;
  
  @Column(name="email",nullable=false)
  private String email;
  
  @Column(name="password",nullable=false)
  private String password;
  
  @Column(name="first_name")
  private String firstName;
  
  @Column(name="last_name")
  private String lastName;
  
  @OneToMany(mappedBy="user")
  private List&lt;Address&gt; addresses = new ArrayList&lt;&gt;();
  }
  
  
  
  @Entity
  @Table(name="tblAddress")
  public class Address {
  
  @Id
  @GeneratedValue
  @Column
  private Long id;
  
  @Column
  private String streat;
  
  @Column
  private String number;
  
  @ManyToOne(fetch=FetchType.EAGER)
  private User user;
  }
  
  
  
  public interface UserService {
  List&lt;User&gt; findAll(); 
  }
  
  
  
  @Service
  public class JpaUserService implements UserService {
  @Autowired
  private UserRepository userRepository;
  
  @Override
  public List&lt;User&gt; findAll() {
      return userRepository.findAll();
      }
  }
  
  
  
  @Controller
  @RequestMapping(value="/api/users")
  public class ApiUserController {
  
  @Autowired
  private UserService userService;
  
  @Autowired
  private UserDTOToUser toUser;
  
  @Autowired
  private UserToUserDTO toDto;
  
  @RequestMapping(method=RequestMethod.GET)
  ResponseEntity&lt;List&lt;UserDTO&gt;&gt; getUser()
  
  List&lt;User&gt; users = userService.findAll();
  
  if(users == null || users.isEmpty()){
      return new ResponseEntity&lt;&gt;(HttpStatus.NOT_FOUND);
  }
  
  return new ResponseEntity&lt;&gt;(toDto.convert(users), HttpStatus.OK);
      }
  }
  
  
  
  @Repository
  public interface UserRepository extends JpaRepository&lt;User, Long&gt; {
  
  @Query("select u.id, a.number FROM User u LEFT JOIN u.addresses a WHERE u.id = a.user")
  List&lt;User&gt; findAll();
  }
  </code></pre>
  
  <p>When I try to run my code in <code>RestClient</code> on my localhost, I get this exception: </p>
  
  <pre><code>{
  "timestamp": 1511105123172,
  "status": 500,
  "error": "Internal Server Error",
  "exception": "java.lang.ClassCastException",
  "message": "[Ljava.lang.Object; cannot be cast to jwd.wafepa.model.User",
  "path": "/api/users"
  }
  </code></pre>
  
  <p>Just to mention, that <code>UserDTO</code> is the <code>User</code> class without password parameter.</p>
  
  <p>How to write proper JOIN query in JPQL?</p>