title: Empty join table for ManyToMany Relationship
body: <p>A similar question with a different issue has been answered before. I have a bidirectional <code>@ManyToMany</code> relationship using Hibernate. The data persists to the separate entity tables, but the <code>id</code> column that maps to the entity tables is empty. I have tried the suggestions in <a href="https://stackoverflow.com/questions/3439392/empty-join-table-resulting-from-jpa-manytomany">Empty Join Table resulting from JPA ManyToMany</a>, <a href="https://stackoverflow.com/questions/20728387/manytomany-relationship-deleting-records-from-relationship-table">ManyToMany relationship, deleting records from relationship table</a> and <a href="https://stackoverflow.com/questions/7248668/hibernate-many-to-many-association-left-hand-side-collection-contains-elements">Hibernate Many-to-many association: left hand side collection contains elements, but right hand side collection is empty</a>, but have not had success so far. Below is my code for completeness (I am still learning java and the spring framework, so I apologise for any rookie mistakes in advance):</p>
  
  <p>CaseStudy Class</p>
  
  <pre><code> @Entity
  public class CaseStudy {
  
      @Id
      @GeneratedValue (strategy = GenerationType.AUTO)
      private Long caseStudyId;
      private String location;
      private String equipment;
      private String issue;
      private String solution;
      private String benefit;
      private float upper_dissolved_Oxygen;
      private float lower_dissolved_Oxygen;
      private float upper_pH;
      private float lower_pH;
      private float upper_temp;
      private float lower_temp;
      private float upper_conductivity;
      private float lower_conductivity;
  
      @CreationTimestamp
      private Date created;
  
      @ManyToOne
      @JsonBackReference
      private User user;
  
      private int likes;
  
      @ManyToMany(mappedBy="casestudyList", fetch = FetchType.EAGER)
      private List&lt;WaterQuality&gt; waterQualityList  = new List&lt;WaterQuality&gt;();
  
      @OneToMany(mappedBy = "casestudy", fetch = FetchType.EAGER)
      private List&lt;Comment&gt; commentList;
  
      public Long getCaseStudyId() {
          return caseStudyId;
      }
  
  
      public void setCaseStudyId(Long caseStudyId) {
          this.caseStudyId = caseStudyId;
      }
  
  
      public String getLocation() {
          return location;
      }
  
  
      public void setLocation(String location) {
          this.location = location;
      }
  
  
      public String getIssue() {
          return issue;
      }
  
  
      public void setIssue(String issue) {
          this.issue = issue;
      }
  
  
      public String getSolution() {
          return solution;
      }
  
  
      public void setSolution(String solution) {
          this.solution = solution;
      }
  
  
      public String getBenefit() {
          return benefit;
      }
  
  
      public void setBenefit(String benefit) {
          this.benefit = benefit;
      }
  
  
      public Date getCreated() {
          return created;
      }
  
  
      public void setCreated(Date created) {
          this.created = created;
      }
  
  
      public User getUser() {
          return user;
      }
  
  
      public void setUser(User user) {
          this.user = user;
      }
  
  
      public int getLikes() {
          return likes;
      }
  
  
      public void setLikes(int likes) {
          this.likes = likes;
      }
  
  
      public List&lt;Comment&gt; getCommentList() {
          return commentList;
      }
  
  
      public void setCommentList(List&lt;Comment&gt; commentList) {
          this.commentList = commentList;
      }
  
  
      public String getEquipment() {
          return equipment;
      }
  
  
      public void setEquipment(String equipment) {
          this.equipment = equipment;
      }
  
  
      public float getUpper_dissolved_Oxygen() {
          return upper_dissolved_Oxygen;
      }
  
  
      public void setUpper_dissolved_Oxygen(float upper_dissolved_Oxygen) {
          this.upper_dissolved_Oxygen = upper_dissolved_Oxygen;
      }
  
  
      public float getLower_dissolved_Oxygen() {
          return lower_dissolved_Oxygen;
      }
  
  
      public void setLower_dissolved_Oxygen(float lower_dissolved_Oxygen) {
          this.lower_dissolved_Oxygen = lower_dissolved_Oxygen;
      }
  
  
      public float getUpper_pH() {
          return upper_pH;
      }
  
  
      public void setUpper_pH(float upper_pH) {
          this.upper_pH = upper_pH;
      }
  
  
      public float getLower_pH() {
          return lower_pH;
      }
  
  
      public void setLower_pH(float lower_pH) {
          this.lower_pH = lower_pH;
      }
  
  
      public float getUpper_temp() {
          return upper_temp;
      }
  
  
      public void setUpper_temp(float upper_temp) {
          this.upper_temp = upper_temp;
      }
  
  
      public float getLower_temp() {
          return lower_temp;
      }
  
  
      public void setLower_temp(float lower_temp) {
          this.lower_temp = lower_temp;
      }
  
  
      public float getUpper_conductivity() {
          return upper_conductivity;
      }
  
  
      public void setUpper_conductivity(float upper_conductivity) {
          this.upper_conductivity = upper_conductivity;
      }
  
  
      public float getLower_conductivity() {
          return lower_conductivity;
      }
  
  
      public void setLower_conductivity(float lower_conductivity) {
          this.lower_conductivity = lower_conductivity;
      }
  
      public List&lt;WaterQuality&gt; getWaterQualityList() {
          return waterQualityList;
      }
  
  
      public void setWaterQualityList(List&lt;WaterQuality&gt; waterQualityList) {
          this.waterQualityList = waterQualityList;
      }
  
  
  }
  </code></pre>
  
  <p><strong>WaterQuality Class</strong></p>
  
  <pre><code>    @Entity
  public class WaterQuality {
  
      @Id
      @GeneratedValue(strategy=GenerationType.AUTO)
      private Long waterQualityId;
      private String description;
      private String status;
      private float dissolved_Oxygen;
      private float pH;
      private float temp;
      private float conductivity;
  
      @CreationTimestamp
      private Date publishDate;
  
      @ManyToOne
      @JsonBackReference
      private User user;
  
      @ManyToMany(targetEntity = CaseStudy.class, cascade = {CascadeType.ALL})
      @JoinTable(
                 joinColumns = {@JoinColumn(name="water_quality_id")},
                 inverseJoinColumns = {@JoinColumn(name="case_study_id")})
      private Set&lt;CaseStudy&gt; casestudyList = new HashSet&lt;CaseStudy&gt;();
  
      public Long getWaterQualityId() {
          return waterQualityId;
      }
  
      public void setWaterQualityId(Long waterQualityId) {
          this.waterQualityId = waterQualityId;
      }
  
      public String getDescription() {
          return description;
      }
  
      public void setDescription(String description) {
          this.description = description;
      }
  
      public String getStatus() {
          return status;
      }
  
      public void setStatus(String status) {
          this.status = status;
      }
  
      public float getDissolved_Oxygen() {
          return dissolved_Oxygen;
      }
  
      public void setDissolved_Oxygen(float dissolved_Oxygen) {
          this.dissolved_Oxygen = dissolved_Oxygen;
      }
  
      public float getpH() {
          return pH;
      }
  
      public void setpH(float pH) {
          this.pH = pH;
      }
  
      public float getTemp() {
          return temp;
      }
  
      public void setTemp(float temp) {
          this.temp = temp;
      }
  
      public float getConductivity() {
          return conductivity;
      }
  
      public void setConductivity(float conductivity) {
          this.conductivity = conductivity;
      }
  
      public Date getPublishDate() {
          return publishDate;
      }
  
      public void setPublishDate(Date publishDate) {
          this.publishDate = publishDate;
      }
  
      public User getUser() {
          return user;
      }
  
      public void setUser(User user) {
          this.user = user;
      }
  
      public Set&lt;CaseStudy&gt; getCasestudyList() {
          return casestudyList;
      }
  
      public void setCasestudyList(Set&lt;CaseStudy&gt; casestudyList) {
          this.casestudyList = casestudyList;
      }
  
      public List&lt;CaseStudy&gt; getDissolvedOxygenalert() {
          List&lt;CaseStudy&gt; dissolvedOxygenalert = casestudyList.stream()           //convert list to stream
                  .filter(casestudy -&gt; casestudy.getUpper_dissolved_Oxygen() &lt; getDissolved_Oxygen() ||  casestudy.getLower_dissolved_Oxygen() &gt; getDissolved_Oxygen())
                  .collect(Collectors.toList());          //collect the output and convert streams to a List
          System.out.print(dissolvedOxygenalert);
          return dissolvedOxygenalert;
      }
  
  
  }
  </code></pre>
  
  <p>I have tried deleting the database and creating it again, but it hasn't solved the problem so far.</p>
  
  <p>Below is the code for persisting the data to the database
  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++</p>
  
  <p>CaseStudy Service Implementation</p>
  
  <pre><code>import java.util.List;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  
  import com.scrunch.data.dao.CaseStudyDao;
  import com.scrunch.data.model.CaseStudy;
  import com.scrunch.data.model.User;
  import com.scrunch.data.service.CaseStudyService;
  
  @Service
  public class CaseStudyServiceImpl implements CaseStudyService{
  
  @Autowired
  private CaseStudyDao casestudyDao;
  
  public CaseStudy save(CaseStudy casestudy) {
      return casestudyDao.save(casestudy);
  }
  
  public Set&lt;CaseStudy&gt; findByUser(User user) {
      return casestudyDao.findByUser(user);
  }
  
  public CaseStudy findByCaseStudyId(Long caseStudyId) {
      return casestudyDao.findByCaseStudyId(caseStudyId);
  }
  
  public Set&lt;CaseStudy&gt; findAll() {
      return casestudyDao.findAll();
  }
  }
  </code></pre>
  
  <p>WaterQuality Service Implementation</p>
  
  <pre><code>import java.util.Date;
  import java.util.List;
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  
  import com.scrunch.data.dao.WaterQualityDao;
  import com.scrunch.data.model.User;
  import com.scrunch.data.model.WaterQuality;
  import com.scrunch.data.service.WaterQualityService;
  
  @Service
  public class WaterQualityServiceImpl implements WaterQualityService{
  
  //  private static final Logger LOGGER =     LoggerFactory.getLogger(WaterQualityServiceImpl.class);
  
  @Autowired
  private WaterQualityDao waterQualityDao;
  
  
  @Override
  public List&lt;WaterQuality&gt; listAllWaterQualityByUserAndDescription(
          User user, String description) {
      return waterQualityDao.findAllWaterQualityByUserAndDescriptionContaining(user, description);
  }
  
  @Override
  public WaterQuality save(WaterQuality waterQuality) {
      return waterQualityDao.save(waterQuality);
  }
  
  @Override
  public List&lt;WaterQuality&gt; findByUser(User user) {
      return waterQualityDao.findByUser(user);
  }
  
  @Override
  public WaterQuality findWaterQualityById(Long waterQualityId) {
      return waterQualityDao.findByWaterQualityId(waterQualityId);
  }
  
  @Override
  public List&lt;WaterQuality&gt; findAll() {
      return waterQualityDao.findAll();
  }
  }
  </code></pre>
  
  <p>WaterQuality Controller</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.HashSet;
  import java.util.List;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.web.bind.annotation.RequestBody;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.RestController;
  
  import com.fasterxml.jackson.annotation.JsonIgnore;
  import com.scrunch.data.model.CaseStudy;
  import com.scrunch.data.model.User;
  import com.scrunch.data.model.WaterQuality;
  import com.scrunch.data.service.WaterQualityService;
  
  @RestController
  @RequestMapping("/rest")
  public class WaterQualityResource {
  private String location;
  private String equipment;
  private String issue;
  private String solution;
  private String benefit;
  private float upper_dissolved_Oxygen;
  private float lower_dissolved_Oxygen;
  private int likes;
  
  
  
  @Autowired
  private WaterQualityService waterQualityService;
  
  @RequestMapping(value="/waterquality/add", method=RequestMethod.POST)
  
  public WaterQuality addWaterQuality(@RequestBody WaterQuality waterQuality) {
  
      waterQuality.setCasestudyList(new HashSet&lt;CaseStudy&gt;());
  
      CaseStudy casestudy = new CaseStudy();
      casestudy.setLocation(location);
      casestudy.setEquipment(equipment);
      casestudy.setIssue(issue);
      casestudy.setSolution(solution);
      casestudy.setBenefit(benefit);
      casestudy.setUpper_dissolved_Oxygen(upper_dissolved_Oxygen);
      casestudy.setLower_dissolved_Oxygen(lower_dissolved_Oxygen);
  
  
      waterQuality.getCasestudyList().add(casestudy);
      return waterQualityService.save(waterQuality);
  }
  </code></pre>
  
  <p>Get request for all waterqualities response returns and endless loop of the same results.</p>
  
  <pre><code>     [{"waterQualityId":1,"description":"sdfdsf","status":"dsfdsf","dissolved_Oxygen":5.0,"pH":7.0,"temp":72.0,"conductivity":500.0,"publishDate":1472784062000,"dissolvedOxygenalert":[{"caseStudyId":2,"location":null,"equipment":null,"issue":null,"solution":null,"benefit":null,"upper_dissolved_Oxygen":0.0,"lower_dissolved_Oxygen":0.0,"upper_pH":0.0,"lower_pH":0.0,"upper_temp":0.0,"lower_temp":0.0,"upper_conductivity":0.0,"lower_conductivity":0.0,"created":1472784062000,"likes":0,"waterQualityList":[{"waterQualityId":1,"description":"sdfdsf","status":"dsfdsf","dissolved_Oxygen":5.0,"pH":7.0,"temp":72.0,"conductivity":500.0,"publishDate":1472784062000,"dissolvedOxygenalert":[{"caseStudyId":2,"location":null,"equipment":null,"issue":null,"solution":null,"benefit":null,"upper_dissolved_Oxygen":0.0,"lower_dissolved_Oxygen":0.0,"upper_pH":0.0,"lower_pH":0.0,"upper_temp":0.0,"lower_temp":0.0,"upper_conductivity":0.0,"lower_conductivity":0.0,"created":1472784062000,"likes":0,"waterQualityList":
  </code></pre>
  
  <p>I am able to get data populated to the join table, but it seems to populate endlessly. It happens when the water quality data is added.</p>