James111 James111 - 5 months ago 7
MySQL Question

How to map many dates to a booking in hibernate and avoid redundancies?

Lets just say that a booking can have many dates! I think my code is a bit redundant as I'm having to insert a booking_date into a booking AND a booking into a booking_date.

Take the following structure, a booking can have 1 or many dates attached to it (in this case, to you it may not make much sense.)

Booking_date:



@Entity
@Table(name="booking_date")
public class BookingDate {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="booking_date_id")
private int id;

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@Column(name="start_date")
private Date startDate;

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@Column(name="end_date" )
private Date endDate;

@ManyToOne
@JoinColumn(name = "booking_id")
private Booking booking;


Booking:



@Entity
@Table(name="booking")
public class Booking {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="booking_id")
private int bookingId;

// One booking to one booking date...
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "booking")
private BookingDate bookingDate;


I think I'm doing something wrong here, as when I go to insert a booking into the database I'd have to insert the booking into the booking_date & visa versa.

Lets take the following example out of my code...Inserting a booking with multiple dates.

@RequestMapping(value="create/booking", method=RequestMethod.POST, produces = {MediaType.APPLICATION_JSON_VALUE })
Booking bookingCreate(@RequestBody BookingContext bookingcont, BindingResult bindingResult) {

Booking booking = new Booking();


I've created the booking entity, now I need to insert a bunch of dates to that booking. First I have to loop through the
List
of
BookingDate
's and set the booking we created above to each of them (I feel this can be avoided)?

List<BookingDate> bookingDates = <Pretend this is a list of BookingDate objects>
// Loop through bookingdates and assign the booking obj to each of them
for (BookingDate bookingDate : bookingDates) {
bookingDate.setBookingDate(bookingDate);
}


I would then go and assign the bookingDates object to our booking object:

booking.setBookingDate(bookingDates); // Insert multiple booking dates for the booking

Answer

This is how it should be

Booking booking = new Booking();
//setting data in booking
List<BookingDate> bookingDates = <Pretend this is a list of BookingDate objects>
booking.setBookingDate(bookingDates);
session.save(booking);

The reason this should work is because there is Cascade.ALL on booking hence saving booking should save the associated bookingdate

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "booking")
private BookingDate bookingDate;

Also you dont need to set booking in each bookingdate as the cascade is from booking not from bookingdate hence the booking needs to know that there exists a list of bookingdate not vice versa. Hope this helps

Comments