0

Right now I have 3 tables in my database - Booking, Restaurant and RestaurantTable. I have a one to many mapping between Restaurant and RestaurantTable (a restaurant can have many tables, but a table can have only one restaurant). I have a file called "editRestaurant.jsp" where (among other things) I would like to display all the tables currently in the restaurant in a drop-down list. I would like to do something like I did in "newBooking.jsp":

            <td><form:select path="restaurant.id">
                    <form:option value="" label="--- Select ---" />
                    <form:options items="${restaurants}" itemValue="id" itemLabel="restaurantName" />
            <td><form:errors path="restaurant.id" cssClass="error"/></td>
            </form:select>

The problem is, I don't know how to handle One to Many mappings, as in specifically, I don't know how to deal with the RestaurantTable set in Restaurant.java. I hope you can understand what I mean and can help me.

My Restaurant.java:

@Entity
@Table(name="restaurant")
public class Restaurant {

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

    @Column(name="restaurant_name")
    private String restaurantName;

    @Column(name="address")
    private String address;

    @OneToMany(mappedBy="restaurant", cascade = CascadeType.ALL)
    private Set<RestaurantTable> table;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getRestaurantName() {
        return restaurantName;
    }

    public void setRestaurantName(String restaurantName) {
        this.restaurantName = restaurantName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Set<RestaurantTable> getTable() {
        return table;
    }

    public void setTable(Set<RestaurantTable> table) {
        this.table = table;
    }

    public String toString() {
        return restaurantName;
    }
}

My RestaurantTable.java:

@Entity
@Table(name="restaurant_table")
public class RestaurantTable {

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

    @Column(name="table_size")
    private int tableSize;

    @Column(name="table_number")
    private int tableNumber;

    @ManyToOne
    @JoinColumn(name="restaurant_id")
    private Restaurant restaurant;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public int getTableSize() {
        return tableSize;
    }

    public void setTableSize(int tableSize) {
        this.tableSize = tableSize;
    }

    public int getTableNumber() {
        return tableNumber;
    }

    public void setTableNumber(int tableNumber) {
        this.tableNumber = tableNumber;
    }

    public Restaurant getRestaurant() {
        return restaurant;
    }

    public void setRestaurant(Restaurant restaurant) {
        this.restaurant = restaurant;
    }

    public String toString() {
        return "Table number " + tableNumber;
    }

}

My current editRestaurant.jsp:

<div id="body">
<section class="content-wrapper main-content clear-fix">

    <h2>Edit</h2>

    <form:form modelAttribute="restaurant">
        <table>
            <tr>
                <td>Restaurant:</td>
                <td><form:input path="restaurantName" /></td>
                <td><form:errors path="restaurantName" cssClass="error"/></td>
            </tr>
            <tr>
                <td>Address:</td>
                <td><form:input path="address" /></td>
                <td><form:errors path="address" cssClass="error"/></td>
            </tr>
                <!--I want to put the list of tables here.-->
            <tr>
                <td colspan="3"><input type="submit" value="Submit" name="submit"/>
                </td>
            </tr>
        </table>
    </form:form>
    <div>
        <a href="/bookings">Back to List</a>
    </div>


</section>

Any help is appreciated!

charliekelly
  • 456
  • 1
  • 7
  • 22

0 Answers0