Modelling entities in hibernate

68 Views Asked by At

This is use case for an online movie ticket booking system (like book my show) to book tickets.

Requirements

  • There are theaters.
  • Theaters have seats.
  • A show is played in a theater with a movie.
  • Users book seats for show.
  • A (Show + Seat) combination should be unique and booked by only one user. (No need to worry about payment failures / rebookings)

The following entities can be created

########################################

Theater
    - name: String
    - seats: List<Seats> 
Seat
    - name: String
    - theater: Theater
Movie
    - name: String
Show
    - theater: Theater
    - movie: Movie
    - startTime: LocalDateTime
    - endTime: LocalDateTime
User
    - name: String
    - email: String

########################################

Booking
    - user: User
    - bookedShowSeats: List<ShowSeat>
ShowSeat
    - show: Show
    - seat: Seat

########################################

How should this be modelled in hibernate?

Few questions on this:

  1. I don't want to create a new entity for ShowSeat (is this possible using hibernate? To model one-to-many relation without having an entity, I read about @Embeddable but was not able to get it working)
  2. How to model the (Show + Seat) unique constraint?
1

There are 1 best solutions below

3
Daksharaj kamal On

The below model defines entities Theater, Seat, Movie, Show, User and Booking where ShowSeat is marked with @Embeddable to represent a composite primary key.

import javax.persistence.*;

@Entity
public class Theater {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    @OneToMany(mappedBy = "theater", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Seat> seats = new ArrayList<>();

    // Getters and setters
}

@Entity
public class Seat {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    @ManyToOne
    private Theater theater;

    // Getters and setters
}

@Entity
public class Movie {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    // Getters and setters
}

@Entity
public class Show {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne
    private Theater theater;

    @ManyToOne
    private Movie movie;

    private LocalDateTime startTime;

    private LocalDateTime endTime;

    // Getters and setters
}

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    private String email;

    // Getters and setters
}

@Embeddable
public class ShowSeatId implements Serializable {
    private Long showId;

    private Long seatId;

    // getters and setters
}

@Entity
public class Booking {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne
    private User user;

    // @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    // private List<ShowSeat> bookedShowSeats = new ArrayList<>();

    @ElementCollection
    @CollectionTable(name = "booking_show_seats", joinColumns = @JoinColumn(name = "booking_id"))
    private List<ShowSeat> bookedShowSeats = new ArrayList<>();


    // Getters and setters
}

//@Entity
@Embeddable
public class ShowSeat {
    @EmbeddedId
    private ShowSeatId id;

    @ManyToOne
    @MapsId("showId")
    private Show show;

    @ManyToOne
    @MapsId("seatId")
    private Seat seat;

    // Getters and setters
}

Remember to configure your Hibernate and JPA configurations.

I hope this helps ✌