title: How to retrieve data from a many-to-many relationship in hibernate?
body: <p>I have four tables as shown in the below diagram.</p>
  
  <p><a href="https://i.stack.imgur.com/HhrvR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HhrvR.png" alt="enter image description here"></a></p>
  
  <p>The relationship between restaurant and category is many-to-many. For that a new table named restaurant_category is being used. 
  I set the relationship between Restaurant and Categories as below.</p>
  
  <p>RestaurantEntity.java</p>
  
  <p>================</p>
  
  <pre><code>package com.upgrad.FoodOrderingApp.service.entity;
  
  import javax.persistence.*;
  import javax.validation.constraints.Size;
  import java.io.Serializable;
  import java.math.BigDecimal;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.UUID;
  
  @Entity
  @Table(name = "restaurant")
  
  @NamedQueries(
          {       @NamedQuery(name = "getAllRestaurants", query = "select res from RestaurantEntity res")
          }
  )
  
  public class RestaurantEntity implements Serializable {
  
      @Id
      @Column(name = "id")
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Integer id;
  
      @Column(name = "UUID")
      @Size(max = 200)
      private UUID uuid;
  
      @Column(name = "restaurant_name")
      @Size(max = 50)
      private String restaurantName;
  
      @Column(name = "photo_url")
      @Size(max = 225)
      private String photoURL;
  
      @Column(name = "customer_rating")
      private BigDecimal customeRating;
  
      @Column(name = "average_price_for_two")
      private Integer avgPriceForTwo;
  
      public List&lt;CategoryEntity&gt; getCategories() {
          return categories;
      }
  
      public void setCategories(List&lt;CategoryEntity&gt; categories) {
          this.categories = categories;
      }
  
      @Column(name = "number_of_customers_rated")
      private Integer numbrOfCustomersRated;
  
      @ManyToOne(fetch = FetchType.EAGER)
      @JoinColumn(name = "address_id")
      private AddressEntity addressEntity;
  
      @ManyToMany(cascade=CascadeType.ALL)
      @JoinTable(name="restaurant_category", joinColumns={@JoinColumn(referencedColumnName="ID")}
              , inverseJoinColumns={@JoinColumn(referencedColumnName="ID")})
      private List&lt;CategoryEntity&gt; categories = new ArrayList&lt;&gt;();
  
      public Integer getId() {
          return id;
      }
  
      public void setId(Integer id) {
          this.id = id;
      }
  
      public UUID getUuid() {
          return uuid;
      }
  
      public void setUuid(UUID uuid) {
          this.uuid = uuid;
      }
  
      public String getRestaurantName() {
          return restaurantName;
      }
  
      public void setRestaurantName(String restaurantName) {
          this.restaurantName = restaurantName;
      }
  
      public String getPhotoURL() {
          return photoURL;
      }
  
      public void setPhotoURL(String photoURL) {
          this.photoURL = photoURL;
      }
  
      public BigDecimal getCustomeRating() {
          return customeRating;
      }
  
      public void setCustomeRating(BigDecimal customeRating) {
          this.customeRating = customeRating;
      }
  
      public Integer getAvgPriceForTwo() {
          return avgPriceForTwo;
      }
  
      public void setAvgPriceForTwo(Integer avgPriceForTwo) {
          this.avgPriceForTwo = avgPriceForTwo;
      }
  
      public Integer getNumbrOfCustomersRated() {
          return numbrOfCustomersRated;
      }
  
      public void setNumbrOfCustomersRated(Integer numbrOfCustomersRated) {
          this.numbrOfCustomersRated = numbrOfCustomersRated;
      }
  
      public AddressEntity getAddressEntity() {
          return addressEntity;
      }
  
      public void setAddressEntity(AddressEntity addressEntity) {
          this.addressEntity = addressEntity;
      }
  }
  </code></pre>
  
  <p>CategoryEntity.java</p>
  
  <p>===============</p>
  
  <pre><code>package com.upgrad.FoodOrderingApp.service.entity;
  
  import org.apache.commons.lang3.builder.EqualsBuilder;
  import org.apache.commons.lang3.builder.HashCodeBuilder;
  import org.apache.commons.lang3.builder.ToStringBuilder;
  import org.apache.commons.lang3.builder.ToStringStyle;
  
  import javax.persistence.*;
  import javax.validation.constraints.Size;
  import java.io.Serializable;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.UUID;
  
  @Entity
  @Table(name = "category")
  public class CategoryEntity implements Serializable {
  
      @Id
      @Column(name = "ID")
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      public Integer id;
  
      @Column(name = "UUID")
      @Size(max = 200)
      public UUID uuid;
  
      public List&lt;RestaurantEntity&gt; getRestaurants() {
          return restaurants;
      }
  
      public void setRestaurants(List&lt;RestaurantEntity&gt; restaurants) {
          this.restaurants = restaurants;
      }
  
      @Column(name = "CATEGORY_NAME")
      @Size(max = 255)
  
      public String category_name;
  
      @ManyToMany(mappedBy="categories")
      private List&lt;RestaurantEntity&gt; restaurants = new ArrayList&lt;&gt;();
  
      public Integer getId() {
          return id;
      }
  
      public void setId(Integer id) {
          this.id = id;
      }
  
      public UUID getUuid() {
          return uuid;
      }
  
      public void setUuid(UUID uuid) {
          this.uuid = uuid;
      }
  
      public String getCategory_name() {
          return category_name;
      }
  
      public void setCategory_name(String category_name) {
          this.category_name = category_name;
      }
  
      @Override
      public boolean equals(Object obj) {
          return new EqualsBuilder().append(this, obj).isEquals();
      }
  
      @Override
      public int hashCode() {
          return new HashCodeBuilder().append(this).hashCode();
      }
  
      @Override
      public String toString() {
          return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
      }
  }
  </code></pre>
  
  <p>Also I used a namedQuery in RestaurantEntity as below,</p>
  
  <pre><code>@NamedQueries(
          {       @NamedQuery(name = "getAllRestaurants", query = "select res from RestaurantEntity res")
          }
  )
  </code></pre>
  
  <p>This namedQuery returns all the data required for Restaurants,and its related object -address. But it does not return data for its related categories. Am I doing something wrong in setting up the relationship between Restaurants and Categories?. Please guide me.</p>
  
  <p>So what my expectation is that named query should return data for related categories as well.</p>
  
  <p>Note : In terminal I am getting below error,</p>
  
  <blockquote>
    <p>org.postgresql.util.PSQLException: ERROR: column
    categories0_.categories_id does not exist</p>
  </blockquote>