I have User as parent entity and booking as child entity, want to fetch all child along with User, but i dont all fields of user. only mobile email and name fields are required. remaining fields of user not required.
User entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
private String username;
private Long mobile;
private String password;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "user", orphanRemoval = true )
private List<Booking> booking;
}
Booking entity
public class User {
private LocalTime time;
private Boolean isPlayed = false;
private String sport;
@ManyToOne(fetch = FetchType.LAZY)
private User user;
}
repo
public interface BookingRepo extends JpaRepository<Booking, Long> {
@Query("SELECT b FROM Booking b JOIN FETCH b.user u ORDER BY b.id DESC")
Page<Object[]> findAllBookingsWithUserMobileAndName(Pageable pageable);
}
reslut should be like this
[{id:1, time:"2024-04-25", isplayd:true, sport:"cricket, usermobile:98744569874, name:"sam"},
{id:2, time:"2024-04-26", isplayd:true, sport:"cricket, usermobile:987458745, name:"Jhon"}]