How to create a get request for many-to-one columns?

Malik Safwan

I currently have made a spring boot project which is for an event system. In the model for booking class, I have two objects one is an event and the other one is the user. Now I want to create a get request that allows me to get all bookings made by a single user and all the bookings for a single event respectively. I have managed to create the other requests which are getting all the bookings and getting a booking by the booking id.

Right now if I try to make create any sort of implementation it either gives me a null pointer error or tells me the table relation "booking" doesn't exist. Please let me know if it's possible to write such a get request. Thanks

Model:

@Id
@SequenceGenerator(
        name = "booking_sequence",
        sequenceName = "booking_sequence",
        allocationSize = 1
)
@GeneratedValue(
        strategy = GenerationType.SEQUENCE,
        generator = "booking_sequence"
)
private Long id;

@ManyToOne
@JoinColumn(
        name = "event_id",
        referencedColumnName = "id"
)
private Event event;

@ManyToOne
@JoinColumn(
        name = "user_id",
        referencedColumnName = "id"
)
private User user;

private Integer tickets;
@Transient
private Integer amount;

Repository:

@Repository
public interface BookingRepository extends JpaRepository<Booking, Long > {
    @Query
    Optional<Booking> findBookingById(Long id);
}

Service:

@Autowired
public BookingService(BookingRepository bookingRepository) {
    this.bookingRepository = bookingRepository;
}

public List<Booking> getBookingList() {
    return bookingRepository.findAll();
}

public Booking getSingleBooking(Long bookingId) {
    return bookingRepository.findBookingById(bookingId).orElseThrow();
}

Controller:

@GetMapping
public List<Booking> getBookings() {
    return bookingService.getBookingList();
}

@GetMapping(path = "{bookingId}")
public Booking getSingleBooking(@PathVariable("bookingId") Long bookingId) {
    return bookingService.getSingleBooking(bookingId);}

@GetMapping(path = "/user/{userId}")
public List<Booking> getUserBookings(@PathVariable("userId") Long userId) {
    return bookingService.getBookingByUser(userId);}

@GetMapping(path = "/event/{eventId}")
public List<Booking> getEventBookings(@PathVariable("eventId") Long eventId) {
    return bookingService.getBookingForEvent(eventId);}
Malik Safwan

So it is possible to make such requests, all I did was use "nativeQuery" so that it would function the way I want it to. As mentioned I wanted to make two get-requests and here is how I wrote the queries for them.

Getting all user bookings of a specific user using its "ID":

@Query(value = "SELECT * from bookings, user where bookings.user_id = :id", nativeQuery = true)
List<Booking> findByUserid(Long id);

Getting all event bookings of a specific event using its "ID":

@Query(value = "SELECT * from bookings, user where bookings.event_id = :id", nativeQuery = true)
List<Booking> findByEventid(Long id);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get Sum of many columns as one

spring hibernate sql one to many relation, use controller request GET to create entry

How to get one datatable with two columns from another datatable with many columns in C#

How to get all of the one-to-many entities in a GET request with Spring Boot and JPA

How to create a Python Dataframe with columns of dynamic length with one-to-many relation to the first column

How to split one dataframe column into many columns

How to create many columns in Pandas (as with a loop in Stata)?

String Formatting using many pandas columns to create a new one

how to get the bigger value of many columns in laravel

How to create many http servers into one app?

How to create an object in a one to many relationship

MySQL How to create one to Zero or Many relation

Django - How to create many to one relation

How to create a one to many relationship using GraphQL?

how to create the product of many columns into new columns pandas

How to create a one to many relationship based on one to one (three tables)?

Spring boot and mySQL get request for one to many relationship

One fetch request creates many GET requests on server

python how to create many-to-many of lists inside one list

Spring Hibernate Mysql: how to get object for many-to-one, many-to-many, one-to-many

SQL create new column based on the one-to-one or one-to-many relationship between two original columns

how to create multiple objects with one request DRF

How to create new columns from one

How to reshape a wide dataframe (many columns, fewer rows) as a long one (few columns, many rows)?

Generalize Get/Create Stored Procedure From One Item to Many

How to create One-to-Many and Many-to-One Relationship from one class to another class in Java/IntelliJ

How many bytes does one character take on a web page request?

Firestore - How many Documents can I retrieve in one Collection request?

how to cancel one of the pending http request from many angular 8